PHP: Datetime::Diff 结果比较
我试图比较两个日期之间的差异,但结果似乎很错误,例如这段代码:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days')."<br />";
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-15');
$interval2 = $datetime1->diff($datetime2);
echo $interval2->format('%R%a days')."<br />";
if($interval == $interval2){ echo "true"; }else{echo "false"; }
返回true,但在上面你可以看到日期差异不一样,实际上回显打印+2和+4 。知道如何比较 2 个日期差异吗?
编辑: datetime::diff 返回一个 dateinterval 对象,实际上它没有实现比较运算符, https:// bugs.php.net/bug.php?id=49914 我将使用 dateinterval vars 来检查差异,感谢您的回答
i was trying to compare the difference between 2 dates, but it seems the results are pretty wrong, for example this code:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days')."<br />";
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-15');
$interval2 = $datetime1->diff($datetime2);
echo $interval2->format('%R%a days')."<br />";
if($interval == $interval2){ echo "true"; }else{echo "false"; }
Returns true, but above you can see the date differences are not the same, in fact echo prints +2 and +4. Any idea in how to compare 2 date differences?
EDIT: the datetime::diff returns a dateinterval object, in fact it doesn't implement comparison operators, https://bugs.php.net/bug.php?id=49914
I'll use dateinterval vars to check the difference, thanks for the answers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
看来 DateInterval 内部没有实现比较功能。允许扩展为其预定义类定义自定义比较规则。显然,它又回到了松散的比较,即对象属于同一类。
此功能请求提供了一个补丁来添加此功能,但它没有似乎在任何时候都已将其放入源中。
为了解决这个问题,您可以自己比较对象的每个成员变量(年、月等),也可以将每个对象转换为数组:
It seems that DateInterval doesn't implement a comparison function internally. Extensions are allowed to define custom comparison rules for their predefined classes. Evidently it falls back to a loose comparison that the objects are of the same class.
This feature request provides a patch to add this functionality in, but it doesn't seem to have made it into the source at any point.
To get around this issue, you can either compare each member variable of your objects yourself (years, months etc.) or you can cast each object to an array:
您只是比较这两个对象文档 具有相同的类型(以及相同的 props 值),但并不相同:
请注意,您正在进行对象比较,而不是值比较,就像字符串一样。
You are only comparing that the two objectsDocs are of the same type (and same value of props), but not that they are identical:
Take note, that you are doing an object comparison, not a value comparison, like with a string.
我已经扩展了 php 类。方法比较进行值比较。它使用 php DateInterval 类中变量的“自然”顺序。 foreach 循环首先经过几年,然后是几个月,然后是几天,等等。它可能不是一个非常可移植的解决方案,但它似乎在 php 5.3 中工作得很好。
I have extended the php class. Method compare makes a value comparison. It uses the "natural" order of the variables in php DateInterval class. The foreach cycle goes at first through years, then months, then days, etc. It is probably not a very portable solution, but it seems to be working just fine in php 5.3.
您将 $datetime1->diff($datetime2) 分配给 $interval 和 $interval2,因此它们具有完全相同的值
You are assigning $datetime1->diff($datetime2) both to $interval and $interval2, so they have the exact same values
我记得php中有一个比较日期的函数,如下所示。
I remember there is a function for comparing the dates in php like this.
我使用以下方式对两个 DateIntervals 进行了比较:
I made comparison between two DateIntervals using the following way: