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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:11:49  来源:igfitidea点击:

PHP check if timestamp is greater than 24 hours from now

phpdatetimestrtotime

提问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 $dateis 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");

See difference in this CodePad

查看此 CodePad 中的差异

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