I'm currently creating a CI project and i faced the folowing issue. I want to show "posted "X" time ago" text and i found a script online but the seconds and minutes are not properly shown.
I've already searched all over the net but i couldn't find anything.
Here's my function:
$today = time();
$createdday = mysql_to_unix($ptime);
$datediff = abs($today - $createdday);
$difftext = "";
$years = floor($datediff / (365 * 60 * 60 * 24));
$months = floor(($datediff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($datediff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
$hours = floor($datediff / 3600);
$minutes = floor($datediff / 60);
$seconds = floor($datediff);
Here's the full pastebin https://pastebin.com/tzBN2gZW
对此有何想法?
谢谢
答案 0 :(得分:1)
发生此错误是因为您不减少天数后的datediff。但是我认为使用DateTime对象进行这种计算更合适
$today = time();
$createdday = mysql_to_unix($ptime);
$today_d = new DateTime();
$today_d->setTimestamp($today);
$createdday_d = new DateTime();
$createdday_d->setTimestamp($createdday);
print_r($today_d->diff($createdday_d));