php date function calculation not working
Hello i am learning php i tried to use the date function to display the cur开发者_如何学Pythonrent time. I have an if statement to check the current time whether it is greater than the next hour. Here is the code
<?
$current_time= date('G:i:s');
$next_time= date('G:i:s',strtotime('+1hour'));
if($current_time > $next_time )
{
echo $current_time." is greater than ".$next_time;
}
?>
At the time of writing this code it was 9:23:39 and the next hour should have been 10:23:39
Surprisingly i got :
9:23:39 is greater than 10:23:39
Am i missing something here. Please help
Keep in mind you are comparing strings, not numbers. 9 is greater than 1 and therefore returns true.
Either use the DateTime class to do this, or compare the unix timestamps (time()
and strtotime('+1hour')
)
you could use a integer to work the if statement
$var = date("U",timestamp)
to get the number of Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) thus giving you a number which you can determine which one is greater.
精彩评论