【问题标题】:compare two times in php [duplicate]在php中比较两次[重复]
【发布时间】:2012-11-03 16:32:34
【问题描述】:

可能重复:
How to compare two time in PHP

嗨,我是 php 中的时间函数新手,我有,

@$download_time = date('d-m-Y H:i:s', $dec_time);
$current_time = date('d-m-Y H:i:s');    
$diff = abs(strtotime($current_time) - strtotime($download_time));    
$time=65 // $time is in seconds

现在我想比较 if ($diff

【问题讨论】:

  • 请确保在发布新问题之前始终尝试搜索功能。很可能您的问题以前曾遇到过,并且已经存在令人满意的答案。这个特定的问题之前已经被问过并回答了数千次。祝你好运!
  • 谢谢我从下一个...

标签: php time strtotime


【解决方案1】:

如果$dec_time 是一个unix 时间戳,那么很简单:

if (time() - $dec_time < $time) {/* ... */}

否则,如果是文本表示:

if (time() - strtotime($dec_time) < $time) {/* ... */}

如果以后不需要,请注意使用不必要的变量,因为如果注意这一点,您可以节省大量内存。如果服务器的 PHP 版本高于 5.2,那么您可以使用 DateTime 类,它为您提供了很多扩展功能。

我不明白为什么是abs(),因为下载时间应该是过去的。

注意:@ 远离您的代码,因为它会为 PHP 解释器花费大量额外的计算,可能键入时间较长,但请测试该变量之前是否存在,例如:

if (!empty($dec_time) && time() - strtotime($dec_time) < $time) {/* ... */}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多