【问题标题】:How to check timespan PHP function as less than 1 hour, today and yesterday?如何检查时间跨度 PHP 函数少于 1 小时,今天和昨天?
【发布时间】:2016-09-14 04:29:52
【问题描述】:

我尝试将添加的日期显示为 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小时的情况?

【问题讨论】:

    标签: php codeigniter


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2017-01-13
      • 2011-06-14
      • 2012-03-12
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 2012-09-29
      相关资源
      最近更新 更多