php PHP日期比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1841308/
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 04:01:31  来源:igfitidea点击:

PHP date comparison

phpdatetime

提问by stef

How would I check if a date in the format "2008-02-16 12:59:57" is less than 24 hours ago?

如何检查格式为“2008-02-16 12:59:57”的日期是否小于 24 小时前?

回答by Don

if (strtotime("2008-02-16 12:59:57") >= time() - 24 * 60 * 60)
{ /*LESS*/ }

回答by LiraNuna

Just adding another answer, using strtotime's relative dates:

只需添加另一个答案,使用strtotime的相对日期:

$date = '2008-02-16 12:59:57';
if (strtotime("$date +1 day") <= time()) {
    // Do something
}

I think this makes the code much more readable.

我认为这使代码更具可读性。

回答by Dominic Rodger

if ((time() - strtotime("2008-02-16 12:59:57")) < 24*60*60) {
  // less than 24 hours ago
}

回答by Satanicpuppy

Php has a comparison function between two date/time objects, but I don't really like it very much. It can be imprecise.

Php 有两个日期/时间对象之间比较功能,但我不是很喜欢它。它可能不精确。

What I do is use strtotime() to make a unix timestamp out of the date object, then compare it with the output of time().

我所做的是使用 strtotime() 从日期对象中创建一个 unix 时间戳,然后将它与 time() 的输出进行比较。

回答by VolkerK

e.g. via strtotimeand time().
The difference must be less then 86400 (seconds per day).

例如通过strtotime和 time()。
差异必须小于 86400(每天的秒数)。

<?php
echo 'now: ', date('Y-m-d H:i:s'), "\n";
foreach( array('2008-02-16 12:59:57', '2009-12-02 13:00:00', '2009-12-02 20:00:00') as $input ) {
  $diff = time()-strtotime($input);
  echo $input, ' ', $diff, " ", $diff < 86400 ? '+':'-', "\n";
}

prints

印刷

now: 2009-12-03 18:02:29
2008-02-16 12:59:57 56696552 -
2009-12-02 13:00:00 104549 -
2009-12-02 20:00:00 79349 +

only the last test date/time lays less than 24 hours in the past.

只有最后一次测试日期/时间在过去不到 24 小时。

回答by Gaurav Gupta

Just use it.....

就用吧.....

if(strtotime($date_start) >= strtotime($currentDate))
{
// Your code
}

回答by Vishnu Sharma

There should be you variable date Like

应该有你可变日期喜欢

$date_value = "2013-09-12";
$Current_date = date("Y-m-d"); OR $current_date_time_stamp = time();

You can Compare both date after convert date into time-stamp so :

您可以在将日期转换为时间戳后比较两个日期:

if(strtotime($current_date) >= strtotime($date_value)) {
 echo "current date is bigger then my date value";
}

OR

或者

if($current_date_time_stamp >= strtotime($date_value)) {
 echo "current date is bigger then my date value";
}

回答by Ajie Kurniyawan

Maybe it will be more easy to understand...

也许会更容易理解...

$my_date        = '2008-02-16 12:59:57';
$one_day_after  = date('Y-m-d H:i:s', strtotime('2008-02-16 12:59:57 +1 days'));

if($my_date < $one_day_after) {
echo $my_date . " is less than 24 hours ago!";
} else {
echo $my_date . " is more than 24 hours ago!";
}

回答by isa

You can use Simple PHP to do this:

您可以使用简单的 PHP 来执行此操作:

$date = new simpleDate();
echo $date->now()->subtractHour(24)->compare('2008-02-16 12:59:57')->isBefore();