1

我试图显示发表评论的时间。我现在很困惑,我尝试了很多方法,但似乎不起作用。

我的代码只是在几年前作为输出。

这是我的代码:

$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";
}
4

2 回答 2

2

PHP 的 DateTime 类有一个Diff()方法可以完全满足您的要求。

您的代码将如下所示:

$date    = new DateTime();
$rowDate = DateTime::CreateFromFormat('U',$row['time']);

$interval = $date->diff($rowDate);

if($interval->y) {
    echo $interval->format('%y years');
} elseif($interval->m) {
    echo $interval->format('%m months');
} elseif($interval->d) {
    echo $interval->format('%d days');
} elseif($interval->h) {
    echo $interval->format('%h hours');
} elseif($interval->i) {
    echo $interval->format('%i minutes');
} elseif($interval->s) {
    echo $interval->format('%s seconds');
}
于 2013-05-26T21:12:51.500 回答
1

请参阅 cakephp 的 TimeHelpers:

public static function niceShort($dateString = null, $timezone = null) {
  if (!$dateString) {
   $dateString = time();
  }
$date = self::fromString($dateString, $timezone);

if (self::isToday($dateString, $timezone)) {
  return __d('cake', 'Today, %s', self::_strftime("%H:%M", $date));
}
if (self::wasYesterday($dateString, $timezone)) {
  return __d('cake', 'Yesterday, %s', self::_strftime("%H:%M", $date));
}
if (self::isTomorrow($dateString, $timezone)) {
  return __d('cake', 'Tomorrow, %s', self::_strftime("%H:%M", $date));
}

$d = self::_strftime("%w", $date);
$day = array(
__d('cake', 'Sunday'),
__d('cake', 'Monday'),
__d('cake', 'Tuesday'),
__d('cake', 'Wednesday'),
__d('cake', 'Thursday'),
__d('cake', 'Friday'),
__d('cake', 'Saturday')
);
if (self::wasWithinLast('7 days', $dateString, $timezone)) {
  return sprintf('%s %s', $day[$d], self::_strftime(self::$niceShortFormat, $date));
}
if (self::isWithinNext('7 days', $dateString, $timezone)) {
  return __d('cake', 'On %s %s', $day[$d], self::_strftime(self::$niceShortFormat, $date));
}

$y = '';
if (!self::isThisYear($date)) {
  $y = ' %Y';
}
  return self::_strftime(self::convertSpecifiers("%b %eS{$y}, %H:%M", $date), $date);
}

更多信息在这里:

希望它能让您了解如何实现自己的实用程序功能。

于 2013-05-26T18:58:42.663 回答