如何比较 PHP 5.2.8 中的两个 DateTime 对象?

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

How do I compare two DateTime objects in PHP 5.2.8?

phpdatetime

提问by RedBlueThing

Having a look on the PHP documentation, the following two methods of the DateTimeobject would both seem to solve my problem:

查看 PHP 文档,DateTime对象的以下两种方法似乎都可以解决我的问题:

Both these methods are marked in the docoas being available in version >= 5.3 (and, not surprisingly, if I try to call them I find they don't exist). I can't find any specific documentation for 5.2.8 so I am not sure if there are equivalent methods in my version. I have Googledthe problem and found an eclectic range of solutions, none of which answer my very simple requirements:

这两种方法在doco 中都标记为在版本 >= 5.3 中可用(并且,毫不奇怪,如果我尝试调用它们,我会发现它们不存在)。我找不到 5.2.8 的任何特定文档,所以我不确定我的版本中是否有等效的方法。我在谷歌上搜索了这个问题并找到了一系列不拘一格的解决方案,但没有一个能满足我非常简单的要求:

  • How do I compare two DateTime objects?
  • Where can I find the doco for previous PHP versions? Specifically version 5.2.8?
  • 如何比较两个 DateTime 对象?
  • 在哪里可以找到以前 PHP 版本的 doco?特别是5.2.8版?

For some context, I have the following code:

对于某些上下文,我有以下代码:

$st_dt = new DateTime(verifyParam ('start_date'));
$end_dt = new DateTime(verifyParam ('end_date'));

// is the end date more ancient than the start date?
if ($end_dt < $start_dt) 

Apparently there is no comparison operator on this guy.

显然这个人没有比较运算符。

Edit

编辑

Apparentlymy assumptions were completely false (thanks Milen for illustrating this so effectively). There is a comparison operator and it works just fine thanks. Sometimes I really miss a compiler. The bug is in the code above, I am sure you will find it much faster than I did :).

显然我的假设是完全错误的(感谢 Milen 如此有效地说明了这一点)。有一个比较运算符,它工作得很好,谢谢。有时我真的很想念一个编译器。错误在上面的代码中,我相信你会发现它比我做的要快得多:)。

回答by Milen A. Radev

The following seems to confirm that there are comparison operators for the DateTime class:

以下似乎证实了 DateTime 类有比较运算符:

dev:~# php
<?php
date_default_timezone_set('Europe/London');

$d1 = new DateTime('2008-08-03 14:52:10');
$d2 = new DateTime('2008-01-03 11:11:10');
var_dump($d1 == $d2);
var_dump($d1 > $d2);
var_dump($d1 < $d2);
?>
bool(false)
bool(true)
bool(false)
dev:~# php -v
PHP 5.2.6-1+lenny3 with Suhosin-Patch 0.9.6.2 (cli) (built: Apr 26 2009 20:09:03)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
dev:~#

回答by Roberto Alonso

From the official documentation:

官方文档

As of PHP 5.2.2, DateTime objects can be compared using comparison operators.

从 PHP 5.2.2 开始,可以使用比较运算符比较DateTime 对象。

$date1 = new DateTime("now");
$date2 = new DateTime("tomorrow");

var_dump($date1 == $date2); // false
var_dump($date1 < $date2); // true
var_dump($date1 > $date2); // false

For PHP versions before 5.2.2 (actually for any version), you can use diff.

对于 5.2.2 之前的 PHP 版本(实际上适用于任何版本),您可以使用diff

$datetime1 = new DateTime('2009-10-11'); // 11 October 2013
$datetime2 = new DateTime('2009-10-13'); // 13 October 2013

$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days'); // +2 days

回答by Julien

You can also compare epoch seconds :

您还可以比较纪元秒数:

$d1->format('U') < $d2->format('U')

Source : http://laughingmeme.org/2007/02/27/looking-at-php5s-datetime-and-datetimezone/(quite interesting article about DateTime)

来源:http: //laughingmeme.org/2007/02/27/looking-at-php5s-datetime-and-datetimezone/(很有趣的关于DateTime的文章)

回答by blablabla

If you want to compare dates and not time, you could use this:

如果你想比较日期而不是时间,你可以使用这个:

$d1->format("Y-m-d") == $d2->format("Y-m-d")

回答by jens1o

As of PHP 7.x, you can use the following:

从 PHP 7.x 开始,您可以使用以下内容:

$aDate = new \DateTime('@'.(time()));
$bDate = new \DateTime('@'.(time() - 3600));

$aDate <=> $bDate; // => 1, `$aDate` is newer than `$bDate`

回答by Kyle Coots

$elapsed = '2592000';
// Time in the past
$time_past = '2014-07-16 11:35:33';
$time_past = strtotime($time_past);

// Add a month to that time
$time_past = $time_past + $elapsed;

// Time NOW
$time_now = time();

// Check if its been a month since time past
if($time_past > $time_now){
    echo 'Hasnt been a month';    
}else{
    echo 'Been longer than a month';
}

回答by Tarun Gupta

This may help you.

这可能对你有帮助。

$today = date("m-d-Y H:i:s");
$thisMonth =date("m");
$thisYear = date("y");
$expectedDate = ($thisMonth+1)."-08-$thisYear 23:58:00";


if (strtotime($expectedDate) > strtotime($today)) {
    echo "Expected date is greater then current date";
    return ;
} else
{
 echo "Expected date is lesser then current date";
}