PHP检查时间戳是否超过24小时

时间:2013-07-13 04:45:46

标签: php datetime strtotime

我有软件需要确定截止日期时间是否超过24小时。这是我必须测试的代码。

 $date = strtotime("2013-07-13") + strtotime("05:30:00");

 if($date > time() + 86400) {
    echo 'yes';
 } else {
    echo 'no';
 }

我目前的日期和时间是2013-07-13凌晨2点。距离它只有3个小时的路程。 我的数学是10800秒之外。我的功能是返回yes。对我来说,这就是说$date比现在加上86400秒,实际上它只有10800秒。这不应该归还no吗?

3 个答案:

答案 0 :(得分:17)

$date = strtotime("2013-07-13") + strtotime("05:30:00");

应该是

$date = strtotime("2013-07-13 05:30:00");

See difference in this CodePad

答案 1 :(得分:3)

<?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
}
?>

答案 2 :(得分:1)

将日期和时间的值存储在单独的变量中,并在连接变量后使用strtotime()将其转换为Unix时间戳。

<强>代码:

<?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
}

?>