dsiuy42084 2012-08-22 14:51
浏览 36
已采纳

“DateTime-> modify('+ 0 days')”修改DateTime对象

Could anyone elaborate on why the following DateTime->modify() code yields those weird contradicting results, depending on the PHP version (is it a bug ? In which PHP version ?).

I would expect all examples to produce the same date and time the objects were created with, but this is apparently not the case.

The only example that seems to work is the one in the middle where the DateTime object is created from a UNIX timestamp and where the time zone is set afterwards (it cannot be set upon construction because that will be ignored).

PHP 5.4.4 and 5.4.6:

FROM TIMESTAMP, NO TZ:
2012-08-21 22:00:00 GMT+0000 (offset 0)  <-- different 
2012-08-21 23:00:00 GMT+0000 (offset 0)  <-- from this
FROM TIMESTAMP, WITH TZ:
2012-08-22 00:00:00 CEST (offset 7200)
2012-08-22 00:00:00 CEST (offset 7200)
FROM STRING:
2012-08-22 00:00:00 CEST (offset 7200)
2012-08-22 00:00:00 CEST (offset 7200)

PHP 5.2.0:

FROM TIMESTAMP, NO TZ:
2012-08-21 22:00:00 GMT+0100 (offset 7200)
2012-08-21 22:00:00 GMT+0100 (offset 7200)
FROM TIMESTAMP, WITH TZ:
2012-08-22 00:00:00 CEST (offset 7200)
2012-08-22 00:00:00 CEST (offset 7200)
FROM STRING:
2012-08-22 00:00:00 CEST (offset 7200)  <-- different
2012-08-21 23:00:00 CEST (offset 7200)  <-- from this

Code:

<?php
  $tz = new DateTimeZone('Europe/Berlin');

  echo "FROM TIMESTAMP, NO TZ:
";

  $date = new DateTime('@'.strtotime('2012-08-22 00:00:00 CEST'));
  echo $date->format('Y-m-d H:i:s T').' (offset '.$date->getOffset().")
";

  $date->modify('+0 days');
  echo $date->format('Y-m-d H:i:s T').' (offset '.$date->getOffset().")
";

  echo "FROM TIMESTAMP, WITH TZ:
";

  $date = new DateTime('@'.strtotime('2012-08-22 00:00:00 CEST'));
  $date->setTimezone($tz);
  echo $date->format('Y-m-d H:i:s T').' (offset '.$date->getOffset().")
";

  $date->modify('+0 days');
  echo $date->format('Y-m-d H:i:s T').' (offset '.$date->getOffset().")
";

  echo "FROM STRING:
";

  $date = new DateTime('2012-08-22 00:00:00 CEST', $tz);
  echo $date->format('Y-m-d H:i:s T').' (offset '.$date->getOffset().")
";

  $date->modify('+0 days');
  echo $date->format('Y-m-d H:i:s T').' (offset '.$date->getOffset().")
";
?>
  • 写回答

1条回答 默认 最新

  • doutuo7126 2012-08-23 11:06
    关注

    Using DateTime::add() and specifying an appropriate DateInterval "P0D" doesn't change the date. I think it's a bug in PHP, please be so kind and report it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug