如何比较PHP 5.2.8中的两个DateTime对象? [英] How do I compare two DateTime objects in PHP 5.2.8?

查看:158
本文介绍了如何比较PHP 5.2.8中的两个DateTime对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个非常简单的东西,但是我现在已经把这个问题困在这个问题上了,所以这里。

This seems like something that should be pretty straight forward, but I have been stuck page faulting this problem for a while now, so here goes.

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

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

  • DateTime::diff : Get the difference and use that to determine which is more ancient.
  • DateTime::getTimestamp : Get the UNIX timestampt and compare those.

这两种方法都标记在 noreferrer> doco 在版本> = 5.3中可用(而且,如果我尝试调用它们,我发现它们不存在),并不奇怪。我找不到5.2.8的任何具体文档,所以我不知道在我的版本中是否有等效的方法。我有 Google Googled 这个问题,发现了一个折衷的解决方案,没有一个回答我非常简单的要求:

Both these methods are marked in the doco as 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 Googled the problem and found an eclectic range of solutions, none of which answer my very simple requirements:


  • 如何比较两个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.

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

Apparently my 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 :).

我期待着我的尴尬在你的解决方案的简单性

I look forward to my embarrassment at the simplicity of your solution.

编辑:确定,令人尴尬的是...

And sure enough, embarrassment ensues ...

推荐答案

p>以下似乎确认DateTime类有比较运算符:

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

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:~#

这篇关于如何比较PHP 5.2.8中的两个DateTime对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆