compare two times in php [duplicate]

删除回忆录丶 提交于 2019-12-11 17:51:34

问题


Possible Duplicate:
How to compare two time in PHP

Hi I am new in time functions in php, i have,

@$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

Now I want to compare if ($diff < $time) how can I compare with this two values.


回答1:


If $dec_time is a unix timestamp, then simply:

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

Else if it is a textual representation:

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

Be aware of using needless variables if it's doesn't needed later, because you can save a lot of memory if keep an eye of this. If the server's PHP version is above 5.2, then you can use the DateTime class wich gives you a lot of extended functionality.

I didn't understood why is the abs() because the download time should be at the past.

Notice: Keep the @'s away from your code, because it costs a lot of additional calculations for the PHP interpreter, maybe it is longer to type, but test if the variable exists before, like:

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


来源:https://stackoverflow.com/questions/13391362/compare-two-times-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!