PHP 检查时间戳是否大于 24 小时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17627058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP check if timestamp is greater than 24 hours from now
提问by Naterade
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.
我有需要确定截止日期时间是否大于 24 小时的软件。这是我必须测试的代码。
$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
?
我当前的日期和时间是 2013-07-13 凌晨 2 点。正如你所看到的,它只有 3 个小时的路程。在我的数学中,那是 10800 秒。我的功能是返回yes
。对我来说,这是说$date
大于现在加上 86400 秒,而实际上它只有 10800 秒。这不应该返回no
吗?
回答by Yogesh Suthar
$date = strtotime("2013-07-13") + strtotime("05:30:00");
should be
应该
$date = strtotime("2013-07-13 05:30:00");
回答by Naterade
Store the values of date and time in separate variables and convert it into a Unix timestamp using strtotime()
after concatenating the variables.
将日期和时间的值存储在单独的变量中,并strtotime()
在连接变量后使用将其转换为 Unix 时间戳。
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
}
?>
回答by Vijay
<?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
}
?>