我试图显示发表评论的时间。我现在很困惑,我尝试了很多方法,但似乎不起作用。
我的代码只是在几年前作为输出。
这是我的代码:
$date = new DateTime();
$newtime = $date->format('U');
$oldtime = $row['time'];
$diffrence = $newtime - $oldtime;
$date = new DateTime("@$diffrence");
$sect = round($date->format('s'));
$mint = round($date->format('i'));
$hout = round($date->format('H'));
$dayt = round($date->format('d'));
$mont = round($date->format('m'));
$yeat = round($date->format('Y'));
if ($yeat > 1970){
$disptime = "years ago";
} else if ($yeat < 1970) {
$disptime = "$mont months ago";
} else if ($mont < 0) {
$disptime = "$dayt days ago";
} else if ($dayt < 0) {
$disptime = "$hout hours $mint min ago";
} else if ($hout < 0) {
$disptime = "$mint min $sect sec ago";
} else {
$disptime = "$sect sec ago";
}
编辑将其更改为 0 至 1970 后接近解决方案
解决了,但找到教程并在这里制作我自己的代码的其他方式已解决
$start_date = new DateTime($row['timestamp']);
$since_start = $start_date->diff(new DateTime($date->format('Y-m-d H:i:s')));
$yeat = $since_start->y;
$mont = $since_start->m;
$dayt = $since_start->d;
$hout = $since_start->h;
$mint = $since_start->i;
$sect = $since_start->s;
if ($mint == 0 && $hout == 0 && $dayt == 0 && $mont == 0 && $yeat == 0){
$disptime = "$sect sec ago";
} else if ($hout == 0 && $dayt == 0 && $mont == 0 && $yeat == 0) {
$disptime = "$mint min ago";
} else if ($dayt == 0 && $mont == 0 && $yeat == 0) {
$disptime = "$hout hours ago";
} else if ($mont == 0 && $yeat == 0) {
$disptime = "$dayt days ago";
} else if ($yeat == 0) {
$disptime = "$mont months ago";
} else {
$disptime = "$yeat years ago";
}