0

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

2 天不见广告在线 3 天不见广告在线

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

你可以在这个网站上看到,在线时间是广告发布后的 9 天。

4

2 回答 2

1

假设您在圣诞节发布了广告。日期在 mysql 中保存为2012-12-25. 现在你想显示它已经发布了多少天。像这样使用DateTimeDateInterval类。

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

查看更多实际代码

于 2012-12-31T10:49:08.597 回答
-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";
   }
}
?>
于 2012-12-31T10:40:49.040 回答