如何使用php和mysql计算广告发布的时间

时间:2012-12-31 10:39:07

标签: php mysql

大家好,                有人可以告诉我如何计算在线帖子的时间。意味着我有一个广告网站,我想算一下如何计算广告的在线时间,例如

2天广告在线 3天广告在线

这是一个例子 http://www.buyandsell.ie/motors/classic-cars/kerry/head-gasket-sealer-3

您可以在此网站上看到,自广告发布以来,在线时间为9天。

2 个答案:

答案 0 :(得分:1)

假设您在Christmass上发布了一则广告。日期以2012-12-25的形式保存在mysql中。现在您要显示已发布的天数。像这样使用DateTimeDateInterval类。

$d = DateTime::createFromFormat("Y-m-d", "2012-12-25");
$interval = $d->diff(new DateTime());
echo $interval->format("%a days"); // echos '6 days'

查看更多code in action

答案 1 :(得分:-1)

<?php

//time() will give current time and
//$time will have the time from database when the post was posted on your site.
$time_difference = time() - $time ;

//calculate the difference and show accordingly.
$seconds = $time_difference ;
$minutes = round($time_difference / 60 );
$hours = round($time_difference / 3600 );
$days = round($time_difference / 86400 );
$weeks = round($time_difference / 604800 );
$months = round($time_difference / 2419200 );
$years = round($time_difference / 29030400 );
if($seconds <= 60)
{
    echo "<font id='big'>a few seconds ago</font>";
}
//Minutes
else if($minutes <=60)
{
    if ($minutes==1) {
        echo "1 minute ago";
    }
    else {
        echo $minutes." minutes ago";
    }
}
else if($hours <=24) {
    if ($hours==1) {
        echo "1 hour ago";
    }
    else {
        echo $hours." hours ago";
    }
}
else if($days <= 7)
{
    if ($days==1) {
        echo "Yesterday";
    }
    else {
        echo $days." days ago";
    }
}
else if($weeks <= 4)
{
    if ($weeks==1) {
        echo "1 week ago";
    }
    else {
        echo $weeks." weeks ago";
    }
}
else if($months <=12)
{
    if ($months==1) {
        echo "1 month ago";
    }
    else {
        echo $months." months ago";
    }
}
else
{
    if ($years==1) {
        echo "1 year ago";
    }
    else {
        echo $years." years ago";
   }
}
?>