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.

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

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面