问题
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