DateTime::createFromFormat() - php 日期时间类
DateTime::createFromFormat()
date_create_from_format
(PHP 5 >= 5.3.0, PHP 7)
根据给定的格式解析日期时间字符串
说明
面向对象风格publicstaticDateTime::createFromFormat(string $format,string $time[,DateTimeZone$timezone]): DateTime过程化风格
date_create_from_format(string $format,string $time[,DateTimeZone$timezone]): DateTime
将$time参数给定的日期时间字符串,根据$format参数给定的格式解析为一个新的 DateTime 对象。
参数
$format在解析日期时间字符串的时候使用的格式string。参加下列的格式清单。大部分格式和date()函数中的格式是一致的。
$format中的字符 | 解释 | 示例 |
---|---|---|
日 | --- | --- |
d和j | 一个月中的第几天,2 位数字表示,有前导 0 或者无前导 0 | 01到31或者1到31 |
D和l | 星期几的文字表示 | Mon到Sun或者Sunday到Saturday |
S | 2 个字母表示的一个月中的第几天(序数词),在进行解析的时候会被忽略 | st,nd,rd或者th。 |
z | 一年中的第几天,从 0 开始 | 0到365 |
月 | --- | --- |
F和M | 文本表示的月份,例如 January 或者 Sept | January到December或者Jan到Dec |
m和n | 数值表示的月份,有前导 0 或者无前导 0 | 01到12or1到12 |
年 | --- | --- |
Y | 4 位数字表示的年 | 例如:1999或2003 |
y | 2 位数字表示的年,可用的范围是 1970 至 2069(不含) | 例如:99或03(表示1999和2003) |
时间 | --- | --- |
a和A | 上午、下午 | am或pm |
gandh | 12 小时制的小时,有前导 0 或者无前导 0 | 1到12或者01到12 |
G和H | 24 小时制的小时,有前导 0 或者无前导 0 | 0到23或00到23 |
i | 分钟,有前导 0 | 00到59 |
s | 秒,有前导 0 | 00到59 |
u | 微秒,最多到 6 位数字 | 示例:45,654321 |
时区 | --- | --- |
e,O,P和T | 时区名称,或者是以 UTC 时区为基准的小时偏移量,或者是以 UTC 为基准的小时和分钟的偏移量,小时和分钟之间用冒号(:)分隔。 | 示例:UTC,GMT,Atlantic/Azores或+0200或+02:00或EST,MDT |
完整的日期和时间 | --- | --- |
U | 从 Unix Epoch (January 1 1970 00:00:00 GMT)开始计算的时间,以秒为单位 | 示例:1292177455 |
空白字符和分隔字符 | --- | --- |
(空格) | 一个空格字符或者一个 tab 字符 | 示例: |
# | 可以是一下分隔符号中的任意一个:;,:,/,.,,,-,(或) | 示例:/ |
;,:,/,.,,,-,(或) | 特殊字符 | 示例:- |
? | 随机字节 | 示例:^(需要注意的是,对于 UTF-8 字符,可能会需要多个?。这种情况下,请使用*) |
* | 随机字节,直到遇到下一个有效的分隔符号或者数值 | 示例:使用Y-*-d格式用来解析2009-aWord-08字符串的时候,*会匹配aWord |
! | 将所有的字段(年、月、日、时、分、秒、微秒以及时区)重置到 Unix Epoch 时间。 | 如果不使用!,格式,那么所有的字段会被设置为系统当前的日期和时间。 |
| | 将尚未被解析的字段,也即格式字符串中未明确指定的字段(年、月、日、时、分、秒、微秒以及时区)重置到 Unix Epoch 时间。 | Y-m-d|会解析日期时间字符串中的年、月和日,但是对于时、分、秒字段会设置为 0. |
+ | 在格式字符串中使用这个格式表示字符,并且所提供的日期时间字符串中包含除了格式字符之外的其他数据的话,不会发出一个错误,而是发出一个警告。 | 使用DateTime::getLastErrors()方法来检测所给定的日期时间字符串中是否包含格式字符串指定的内容之外的数据。 |
如果在格式字符串中包含不可识别的字符,那么会导致解析失败,并且在返回的结构中附加一个错误信息。可以通过DateTime::getLastErrors()来探查解析是否存在错误。
如果需要在格式字符串$format参数中使用上述表示格式的字符作为一个普通字符,请对其使用反斜线()进行转义。
如果格式字符串参数$format中不包含!字符,那么没有在$format参数中指明的字段,在解析结果中将会被设置为系统当前时间对应的字段值。
如果格式字符串参数$format包含了!字符,那么没有在$format参数中指明的字段,以及在!左侧对应的字段,在解析结果中将会被设置为 Unix epoch 时间对应的字段。
The Unix epoch 为 1970-01-01 00:00:00 UTC。
$time用来表示日期时间的字符串。
$timezoneDateTimeZone对象,表示在解析日期时间字符串的时候需要使用的时区。
如果忽略$timezone参数,并且表示日期时间的字符串$time中也不包含时区信息,那么将会使用系统当前时区作为解析结果对象的时区。
Note:如果$time参数是 UNIX 时间戳格式(例如:946684800),或者其中已经包含了时区信息(例如:2010-01-28T15:00:00+02:00),那么$timezone以及系统当前时区都将会被忽略。
返回值
返回一个 DateTime 对象。或者在失败时返回FALSE
。
更新日志
版本 | 说明 |
---|---|
5.3.9 | 新增$format格式字符串中对于+格式字符的支持。 |
范例
Example #1DateTime::createFromFormat()例程
面向对象风格
过程化风格
以上例程会输出:
2009-02-15
Example #2DateTime::createFromFormat()的复杂用法
以上例程的输出类似于:
Current time: 2010-04-23 10:29:35 Format: Y-m-d; 2009-02-15 10:29:35 Format: Y-m-d H:i:s; 2009-02-15 15:16:17 Format: Y-m-!d H:i:s; 1970-01-15 15:16:17 Format: !d; 1970-01-15 00:00:00
格式化字符串中包含了需要进行转义的字符
以上例程的输出类似于:
23:15:03
参见
- DateTime::__construct() 返回一个新的 DateTime 对象
- DateTime::getLastErrors() 获取警告和错误信息
checkdate()
验证一个格里高里日期strptime()
解析由 strftime 生成的日期/时间
Be warned that DateTime object created without explicitely providing the time portion will have the current time set instead of 00:00:00. That's also why you can't safely compare equality of such DateTime objects:
If you want to safely compare equality of a DateTime object without explicitly providing the time portion make use of the ! format character. If you omit the ! format character without explicitly providing the time portion your timestamp which will include the current system time in the stamp.
Parsing RFC3339 strings can be very tricky when their are microseconds in the date string. Since PHP 7 there is the undocumented constant DateTime::RFC3339_EXTENDED (value: Y-m-d\TH:i:s.vP), which can be used to output an RFC3339 string with microseconds: But the same constant can't be used for parsing an RFC3339 string with microseconds, instead do: But "u" can only parse microseconds up to 6 digits, but some language (like Go) return more than 6 digits for the microseconds, e.g.: "2017-07-25T15:50:42.456430712+02:00" (when turning time.Time to JSON with json.Marshal()). Currently there is no other solution than using a separate parsing library to get correct dates. Note: the difference between "v" and "u" is just 3 digits vs. 6 digits.
Reportedly, microtime() may return a timestamp number without a fractional part if the microseconds are exactly zero. I.e., "1463772747" instead of the expected "1463772747.000000". number_format() can create a correct string representation of the microsecond timestamp every time, which can be useful for creating DateTime objects when used with DateTime::createFromFormat(): The problem is microtime() and time() returning the timestamp in current timezone. Instead of using time you can use 'now' but to get a DateTimeObject with microseconds you have to write it this way to be sure to get the correct datetime:
If you're here because you're trying to create a date from a week number, you want to be using setISODate, as I discovered here: http://www.lornajane.net/posts/2011/getting-dates-from-week-numbers-in-php
It can be confusing creating new DateTime from timestamp when your default timezone (date.timezone) is different from UTC and you are used to date() function. date() function automatically uses your current timezone setting but DateTime::createFromFormat (or DateTime constructor) does not (it ignores tz-parameter). You can get same results as date() by setting the timezone after object creation.
There is no option to specify date format 'c' (e.g. 2004-02-12T15:19:21+00:00) directly. work around is to use Y-m-d\TH:i:sT
Note that the U option does not support negative timestamps (before 1970). You have to use date for that.
Just a note that it is possible to call createFromFormat non statically. results in: class DateTime#4 (3) { public $date => string(19) "2014-10-01 13:05:05" public $timezone_type => int(3) public $timezone => string(3) "UTC" }
will print: 2015-11-19 11:37:29.125300 (the current time with microseconds)
Be aware: If the day of the month is not provided, creating a DateTime object will produce different results depending on what the current day of the year is. This is because the current system date will be used where values are not provided.
Please note that several points here are wrong. When using microseconds: - The createFromFormat DOES NOT accept the ".v" modifier, unlike the formatting ones. - When you provide microseconds to this function, YOU MUST LEFT PAD THEM to SIX digits with ZEROES. If you don't, anything below 100000 will be RIGHT PADDED with zeroes. (So that 999 becomes 9990000, while 1234 becomes 123400. (Also 0510 becomes 051000.) But at least you can use milliseconds as microseconds. But then again, REMEMBER TO LEFT PAD them to 3 digits if necessary.. - WHAT?? Go provides SEVEN digits of MICROSECONDS?? Something there must definitely be wrong. Micro is, by definition, one millionth, so anything above 999999 is odd.
Seems like setting the hours to any value resets the remaining time portion (minute, second and milliseconds) to zero: This is different from how missing year, month and dates are handled:
To convert an email header date use the following (important, notice the * at the end) $date = DateTime::createFromFormat('D, d M Y H:i:s O *', $email_date); Some dates in email headers can be formatted as: Fri, 12 Jun 2015 13:53:37 +0000 (UTC) The "(UTC)" at the end of the date causes an error and will return a result of false unless the * is at the end.
Beware specifying a timezone in the format as it will take precedence over the DateTimeZone object. This gets hairy when you are playing with transition from summer time to winter time! For instance, in Toronto, the time change happens on 2011-11-06. One second after 01:59:59 (EDT), the time becomes 01:00:00 (EST), or 1320559200 in Unix timestamp. However, notice the following:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!