php date_diff 错误,PHP错误的DateTime :: diff()返回错误的DateInterval

我有两个日期时间差异的问题.以下是显示DateInterval对象的命令行:

php -r "\$a = new Datetime('first day of 4 months ago midnight'); \$b = new Datetime('first day of 1 month ago midnight'); var_dump(\$a->diff(\$b));"

这里是DateInterval输出:

class DateInterval#3 (15) {

public $y => int(0)

public $m => int(3)

public $d => int(3)

public $h => int(0)

public $i => int(0)

public $s => int(0)

public $weekday => int(0)

public $weekday_behavior => int(0)

public $first_last_day_of => int(0)

public $invert => int(0)

public $days => int(92)

public $special_type => int(0)

public $special_amount => int(0)

public $have_weekday_relative => int(0)

public $have_special_relative => int(0)

}

编辑:第一个和第二个Datetime:

class DateTime#1 (3) {

public $date =>

string(19) "2014-03-01 00:00:00"

public $timezone_type =>

int(3)

public $timezone =>

string(13) "Europe/Zurich"

}

class DateTime#2 (3) {

public $date =>

string(19) "2014-06-01 00:00:00"

public $timezone_type =>

int(3)

public $timezone =>

string(13) "Europe/Zurich"

}

注意3天!我使用的是PHP 5.5.8,但我确信这个DateInterval前几天有0个月. DateInterval在PHP 5.4.28和5.5.14中输出0天.我不确定PHP版本是否有效.

在这两种情况下,天属性为92.

解决方法:

$zurich = new DateTimeZone('Europe/Zurich');

$utc = new DateTimeZone('UTC');

$a = new DateTime('first day of 4 months ago midnight',$zurich);

$b = new DateTime('first day of 1 month ago midnight',$zurich);

var_dump($a,$b);

$a->setTimezone($utc);

$b->setTimezone($utc);

var_dump($a,$b);

?>

给出以下内容:

object(DateTime)[3]

public 'date' => string '2014-03-01 00:00:00' (length=19)

public 'timezone_type' => int 3

public 'timezone' => string 'Europe/Zurich' (length=13)

object(DateTime)[4]

public 'date' => string '2014-06-01 00:00:00' (length=19)

public 'timezone_type' => int 3

public 'timezone' => string 'Europe/Zurich' (length=13)

object(DateTime)[3]

public 'date' => string '2014-02-28 23:00:00' (length=19)

public 'timezone_type' => int 3

public 'timezone' => string 'UTC' (length=3)

object(DateTime)[4]

public 'date' => string '2014-05-31 22:00:00' (length=19)

public 'timezone_type' => int 3

public 'timezone' => string 'UTC' (length=3)

3天的差异现在非常明显,在时区从Europe / Zurich转换为UTC后,日期分别为2014-02-28 23:00:00和2014-05-31 22:00:00 $a和$b分别.

解决方案是完全使用UTC并在显示DateTime之前进行转换:

$zurich = new DateTimeZone('Europe/Zurich');

$utc = new DateTimeZone('UTC');

$a = new DateTime('first day of 4 months ago midnight',$utc);

$b = new DateTime('first day of 1 month ago midnight',$utc);

var_dump($a,$b);

$a->setTimezone($zurich);

$b->setTimezone($zurich);

var_dump($a,$b);

?>

请注意,所有日期现在都是01,尽管时间现在有点不同(请参阅本答案末尾的注释):

object(DateTime)[3]

public 'date' => string '2014-03-01 00:00:00' (length=19)

public 'timezone_type' => int 3

public 'timezone' => string 'UTC' (length=3)

object(DateTime)[4]

public 'date' => string '2014-06-01 00:00:00' (length=19)

public 'timezone_type' => int 3

public 'timezone' => string 'UTC' (length=3)

object(DateTime)[3]

public 'date' => string '2014-03-01 01:00:00' (length=19)

public 'timezone_type' => int 3

public 'timezone' => string 'Europe/Zurich' (length=13)

object(DateTime)[4]

public 'date' => string '2014-06-01 02:00:00' (length=19)

public 'timezone_type' => int 3

public 'timezone' => string 'Europe/Zurich' (length=13)

要对此现象提供一些见解,请注意以下事项:

>二月有28天(2014年)

>可能有31天

>夏令时开始于3月30日:01:00

> Europe / Zurich是UTC 02:00

从欧洲/苏黎世转换为UTC时,必须考虑的不仅仅是年,月和日,还要考虑小时,分钟和秒.如果这是除了任何一个月的第一天之外的任何其他日期,则不会发生此问题,但是时间仍然是23:00和22:00(在上面的示例之前).

标签:php,datetime,dateinterval

来源: https://codeday.me/bug/20190825/1714432.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值