PHP check if timestamp is greater than 24 hours from now

旧城冷巷雨未停 提交于 2020-01-14 07:26:23

问题


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

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