DateTime :: diff意外结果

编程入门 行业动态 更新时间:2024-10-26 15:14:01
本文介绍了DateTime :: diff意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下计算,我希望返回0。但是在许多我可以访问的系统上,它返回1:

I have the following calculation, which I expect to return 0. However it returns 1 on many systems I have access to:

Ubuntu 16.04服务器(错误)

Ubuntu 16.04 server (wrong)

php -v PHP 7.0.22-0ubuntu0.16.04.1 (cli) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies with Zend OPcache v7.0.22-0ubuntu0.16.04.1, Copyright (c) 1999- 2017, by Zend Technologies echo "<?php echo DateTime::createFromFormat('Y-m-d H:i:s', '2017-12-01 00:00:00')->diff(DateTime::createFromFormat('Y-m-d H:i:s', '2017-12-31 23:59:59' ))->format('%m');"|php 1

来自deb.sury的PHP 7.1(带有Xdebug)

PHP 7.1 from deb.sury with Xdebug (wrong)

php -v PHP 7.1.6-1~ubuntu16.04.1+deb.sury+1 (cli) (built: Jun 9 2017 08:26:34) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies with Zend OPcache v7.1.6-1~ubuntu16.04.1+deb.sury+1, Copyright (c) 1999-2017, by Zend Technologies with Xdebug v2.5.4, Copyright (c) 2002-2017, by Derick Rethans echo "<?php echo DateTime::createFromFormat('Y-m-d H:i:s', '2017-12-01 00:00:00')->diff(DateTime::createFromFormat('Y-m-d H:i:s', '2017-12-31 23:59:59' ))->format('%m');"|php 1

phpfiddle

phpfiddle

->按预期返回0

日期的时区相同

推荐答案

来自 DateInterval :: format :

DateInterval :: format ()方法不会重新计算时间字符串或日期段中的结转点。这是预料之中的,因为不可能溢出 32天之类的值,该值可以解释为从 1个月4天到 1个月1天的任何值。

The DateInterval::format() method does not recalculate carry over points in time strings nor in date segments. This is expected because it is not possible to overflow values like "32 days" which could be interpreted as anything from "1 month and 4 days" to "1 month and 1 day".

所以必须重新计算结转点。以下是中的相关代码 DateInterval :: format :

So one has to recalculate carry over points. Below is the relevant code from DateInterval::format:

class DateIntervalEnhanced extends DateInterval { public function recalculate() { $from = new DateTime; $to = clone $from; $to->add($this); $diff = $from->diff($to); foreach ($diff as $k => $v) $this->$k = $v; return $this; } }

实用函数:

function myFormatter($d1, $d2, $format) { $diff = strtotime($d1) - strtotime($d2); $df = abs($diff); $di = new DateIntervalEnhanced("PT${df}S"); $di->invert = $diff < 0; return $di->recalculate()->format($format); } echo myFormatter("2017-12-31 23:59:59", "2017-12-01 00:00:00", "%m");

演示

DEMO

链接到您可能想阅读的帖子

Link to a post you might wanna read

更多推荐

DateTime :: diff意外结果

本文发布于:2023-11-13 14:13:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584537.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:意外   DateTime   diff

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!