PHP中的DateTime构造函数 [英] DateTime constructor in php

查看:101
本文介绍了PHP中的DateTime构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对php的 DateTime 类进行了一些实验。在文档中,他们建议了给定DateTime类对象的语法。

I have done a little experiment on php's DateTime class. In the documentation they have suggested the syntax for creating the object of DateTime class as given.

面向对象的样式:

public DateTime::__construct() ( [ string $time = "now" [, DateTimeZone $timezone = NULL ]] )

程序样式:

DateTime date_create ( [ string $time = "now" [, DateTimeZone $timezone = NULL ]] )

返回新的DateTime对象。

Returns new DateTime object.

这里是他们指定为强制性的第一个参数,如日期和时间格式部分。否则,在使用$ timezone参数时,我们必须在此处传递 NULL 来获取当前时间。

Here the first argument they have specified as mandatory, as a date/time string as specified in Date and Time Formats section. Or we have to pass NULL here to obtain the current time when using the $timezone parameter.

但是我的问题是是当我给出以下代码时:

But my problem is when I am giving following code:

date_default_timezone_set('America/Los_Angeles');
$d_ob = new DateTime('x');
echo $d_ob->format('Y-m-d');

应该会生成异常,但它会像-

It's supposed to generate exception, but it's echoing the current date time like -

2013-09-29

我不明白这一点,它是如何工作的?

I am not getting the point, how it's working?

推荐答案

显然,这不是错误,它是预期的 ,但没有记录的行为' 查看对错误报告的评论 。所有单个字母( j除外)都代表军事时区,请参阅一些代码来演示这一点

Apparently, this is not a bug, it is 'expected, but undocumented behaviour'See the comments to the bug report. All single letters (except for 'j') represent military timezones, see some code to demonstrate this.

有更多信息此处

来自 RFC822


军事标准在每个区域使用一个字符。 Z是世界标准时间。 A表示提前一小时, M表示提前十二小时; N是一小时后, Y是十二小时后。

The military standard uses a single character for each zone. "Z" is Universal Time. "A" indicates one hour earlier, and "M" indicates 12 hours earlier; "N" is one hour later, and "Y" is 12 hours later. The letter "J" is not used.

所以回答您的问题

我不明白这一点,它是如何工作的?

I am not getting the point, how it's working?

当DateTime :: __ construct()时传递了一个不是有效时间字符串的单一值,它假定第一个参数已被省略,并尝试将字符串解析为时区。正如RFC解释的那样, x是有效的时区,因此您将获得位于时区 X中的DateTime实例。

When DateTime::__construct() is passed a single value that is not a valid time string it assumes that the first parameter has been omitted and tries to parse the string as a time zone. As the RFC explains, 'x' is a valid timezone, so you will get a DateTime instance that is in timezone 'X'.

我应该提到,尽管构造函数的\DateTime识别这些单个字母区域,\DateTimeZone的构造函数不会!

I should mention that although the constructor of \DateTime recognises these single letter zones, the constructor of \DateTimeZone does not!

我希望能帮上忙。

这篇关于PHP中的DateTime构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆