找出两个时间戳的差异

时间:2013-05-26 18:45:48

标签: php

我正在尝试显示已发布评论的时间。我很困惑,现在我尝试了许多方式,但似乎没有用。

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

这是我的代码:

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

2 个答案:

答案 0 :(得分: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');
}

答案 1 :(得分: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);
}

更多信息:

希望它能让您了解如何实现自己的效用函数。