DateTime :: diff返回不正确的+1小时差异

时间:2014-02-27 10:04:22

标签: php datetime

我必须使用DateTime对象:

object(DateTime)[561]
  public 'date' => string '1970-01-01 11:55:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Europe/Vilnius' (length=14)

object(DateTime)[563]
  public 'date' => string '2014-01-29 11:55:00' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Europe/Vilnius' (length=14)

并且令人惊讶的是DateTime :: diff用于这些返回:

object(DateInterval)[565]
  public 'y' => int 44
  public 'm' => int 0
  public 'd' => int 28
  public 'h' => int 1
  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 16099
  public 'special_type' => int 0
  public 'special_amount' => int 0
  public 'have_weekday_relative' => int 0
  public 'have_special_relative' => int 0

任何可能产生+1小时差异的想法? 使用PHP 5.4.25 先谢谢你们!

2 个答案:

答案 0 :(得分:1)

比较的时间是本地,似乎从{1970}的UTC + 3h到2014年的UTC + 2h,Europe/Vilnius的时区发生了历史性变化:

$ export TZ="Europe/Vilnius"
$ irb
irb(main):001:0> Time.mktime(1970,1,1)
=> Thu Jan 01 00:00:00 +0300 1970
irb(main):002:0> Time.mktime(2014,1,1)
=> Wed Jan 01 00:00:00 +0200 2014

这种变化似乎发生在1990年的某个时候。

语言是ruby,但它使用相同的OS后端作为时间函数和时区数据,如php

答案 1 :(得分:0)

这里的差异没有显示在php 5.3.3 ......:

$ php -v
PHP 5.3.3 (cli) (built: Dec 11 2013 03:29:57)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

<?php
    date_default_timezone_set("Europe/Vilnius");
    $datetime1 = new DateTime('1970-01-01 11:55:00');
    $datetime2 = new DateTime('2014-01-29 11:55:00');
    $interval = $datetime1->diff($datetime2);
    print_r($interval);
?>

给出

    DateInterval Object
(
    [y] => 44
    [m] => 0
    [d] => 28
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 0
    [days] => 16099
)