PHP的strtotime以秒和分钟为单位


php strtotime in seconds and minutes

我使用THS方法查找两个时间戳之间的差异,并获得这两个时间戳之间的秒数,并使用jquery像计数器一样刷新信息。

$diff = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
$time = intval(date('s', $diff));
echo $time;

当时间差大于60秒时,$time返回0,类似于重置。

我想显示1分钟XX秒,例如

date()s标志永远不会返回大于59的值,因为它只表示给定时间的当前秒数,在滚动到新的分钟之前永远不会超过59。

如果想要总秒数,可以删除第二行代码,因为两个Unix时间戳之间的差异总是以秒为单位:

$time = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
echo $time;

如果你想用分和秒来显示,你可以使用DateTime(),它提供了更好的工具:

$now = new DateTime();
$then = new DateTime('2014-06-25 14:50:03');
$diff = $now->diff($then);
echo $diff->format('%i minutes %s seconds');

格式化日期

$diff = strtotime(date('Y-m-d H:i:s')) - strtotime('2014-06-25 14:50:03');
$time = date('i:s', $diff);
echo $time;

像1 &现在2

function diffrencePassTimeAction($DataTime){
    $im = $DataTime - strtotime("now");
  return $im;
}

未来的时间就像2 &现在1

function diffrenceFuturTimeAction($DataTime){
    $im = strtotime("now") - $DataTime;
  return $im;
}

这个函数delete (-less)

function diffrencePassTimeAction($DataTime){
    if ($DataTime > 0)
        return $DataTime - strtotime("now");
    else
        return strtotime("now"); // OR return 0;
}