问题
I have software that needs to determine if the cutoff datetime is greater than 24 hours from now. Here is the code I have to test that.
$date = strtotime("2013-07-13") + strtotime("05:30:00");
if($date > time() + 86400) {
echo 'yes';
} else {
echo 'no';
}
My current date and time is 2013-07-13 2am. As you can see its only 3 hours away.
At my math thats 10800 seconds away. The function I have is returning yes
. To me this is saying the $date
is greater than now plus 86400 seconds when in fact its only 10800 seconds away. Should this not be returning no
?
回答1:
$date = strtotime("2013-07-13") + strtotime("05:30:00");
should be
$date = strtotime("2013-07-13 05:30:00");
See difference in this CodePad
回答2:
<?php
date_default_timezone_set('Asia/Kolkata');
$date = "2014-10-06";
$time = "17:37:00";
$timestamp = strtotime($date . ' ' . $time); //1373673600
// getting current date
$cDate = strtotime(date('Y-m-d H:i:s'));
// Getting the value of old date + 24 hours
$oldDate = $timestamp + 86400; // 86400 seconds in 24 hrs
if($oldDate > $cDate)
{
echo 'yes';
}
else
{
echo 'no'; //outputs no
}
?>
回答3:
Store the values of date and time in separate variables and convert it into a Unix timestamp using strtotime() after concatenating the variables.
Code:
<?php
$date = "2013-07-13";
$time = "05:30:00";
$timestamp = strtotime($date." ".$time); //1373673600
if($timestamp > time() + 86400) {
echo 'yes';
} else {
echo 'no'; //outputs no
}
?>
来源:https://stackoverflow.com/questions/17627058/php-check-if-timestamp-is-greater-than-24-hours-from-now