如何检查时间跨度PHP函数小于1小时,今天和昨天


How to check timespan PHP function as less than 1 hour, today and yesterday?

我试图将添加的日期显示为25分钟前、今天下午5:30等。

$added_time  = strtotime('1 Jan 2016 6:00 AM');
$currentTime = strtotime('1 Jan 2016 7:15 AM'); // probably uses time()
$diff = timespan($time1, $time2);
if($diff < 1 hour){ // how to check 1 hour 
 //display minutes ago
}
else {
 //display added time 
}

条件

  • 如果时间间隔小于60分钟->前25分钟

  • 如果时间间隔超过60分钟,但今天->今天上午6点

  • 如果时间间隔超过60分钟但昨天->昨天上午6点

  • 否则就是$added_time

如何检查今天和昨天不到1小时的情况?

这不是一个特别的CodeIgniter问题。我不确定你到底在做什么,但这段代码会让你很接近。

$Added = new 'DateTime(date('Y-m-d H:i:s', strtotime('1 Jan 2016 6:00 AM')));
$Current = new 'DateTime(date('Y-m-d H:i:s', strtotime('1 Jan 2016 7:15 AM')));
$Diff = $Added->diff( $Current , FALSE);
$hours = $Diff->format('%H');
$mins = $Diff->format('%I');
if( $Diff->invert == true ){
    echo "Some hours $hours and minutes $mins ago ";
}
else if( $Diff->invert == false ){
    echo "Some hours $hours and minutes $mins into the future ";
}

参考文献:

http://php.net/manual/en/class.datetime.php

http://php.net/manual/en/datetime.diff.php