我在php上做了一个小实验DateTime http://php.net/manual/en/datetime.construct.php班级。在文档中,他们建议了创建给定 DateTime 类对象的语法。
面向对象风格:
public DateTime::__construct() ( [ string $time = "now" [, DateTimeZone $timezone = NULL ]] )
程序风格:
DateTime date_create ( [ string $time = "now" [, DateTimeZone $timezone = NULL ]] )
返回新的 DateTime 对象。
这里他们指定为强制的第一个参数,作为指定的日期/时间字符串日期和时间格式 http://www.php.net/manual/en/datetime.formats.php部分。或者我们必须通过NULL
这里使用$timezone参数来获取当前时间。
但我的问题是当我给出以下代码时:
date_default_timezone_set('America/Los_Angeles');
$d_ob = new DateTime('x');
echo $d_ob->format('Y-m-d');
它应该生成异常,但它回显当前日期时间,例如 -
2013-09-29
我没明白重点,它是如何工作的?
Apparently, this is not a bug, it is 'expected, but undocumented behaviour'See the comments to the bug report https://bugs.php.net/bug.php?id=65747. All single letters (except for 'j') represent military timezones, see some code to demonstrate this http://3v4l.org/88HEs.
还有更多信息here http://en.wikipedia.org/wiki/List_of_military_time_zones.
From RFC822 https://www.rfc-editor.org/rfc/rfc822#section-5.2
军事标准对每个区域使用单个字符。 “Z”是世界时间。 “A”表示提前1小时,“M”表示提前12小时; “N”是 1 小时后,“Y”是 12 小时后。不使用字母“J”。
所以回答你的问题
我没明白重点,它是如何工作的?
当 DateTime::__construct() 传递的单个值不是有效的时间字符串时,它假定第一个参数已被省略,并尝试将该字符串解析为时区。正如 RFC 所解释的,“x”是一个有效的时区,因此您将获得时区“X”中的 DateTime 实例。
我应该提到,虽然 \DateTime 的构造函数识别这些单字母区域,但 \DateTimeZone 的构造函数才不是! http://3v4l.org/aO6A6
我希望这有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)