PHP 8.2.4 Released!

DateTime::createFromFormat

date_create_from_format

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

DateTime::createFromFormat -- date_create_from_format根据指定格式解析时间字符串

说明

面向对象风格

public static DateTime::createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTime|false

过程化风格

date_create_from_format(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTime|false

返回新的 DateTime 对象,该对象是通过指定 format 将表示日期和时间的 datetime 格式化生成。

类似于 DateTimeImmutable::createFromFormat()date_create_immutable_from_format(),但创建的是 DateTime 对象。

返回值

返回 DateTime 对象 或者在失败时返回 false

参见

add a note

User Contributed Notes 2 notes

up
6
Uplink
1 month ago
WARNING of subtle bug / feature:

createDateFromFormat (with format 'Y-m-d' for this example) will happily accept '2022-02-29' (February 29 in a non-leap year) and turn it into '2022-03-01' with no warnings, possibly creating weird bugs in your application.
up
3
Julian
1 month ago
Examples:

<?php

// Year-month-day
DateTime::createFromFormat('Y-m-d', '2013-10-14'); // object(DateTime)
DateTime::createFromFormat('Y-m-d', '2013-10-14 09:00'); // bool(false)

// day-month-Year
DateTime::createFromFormat('d-m-Y', '14-10-2013'); // object(DateTime)
DateTime::createFromFormat('d-m-Y', '14-10-2013 09:00'); // bool(false)

// Unix epoch
DateTime::createFromFormat('U', '1381734000'); // object(DateTime)
DateTime::createFromFormat('U', '2013-10-14'); // bool(false)
DateTime::createFromFormat('U', ''); // bool(false)

// The methods below will return an object(DateTime)
DateTime::createFromFormat(DateTimeInterface::ISO8601, '2004-02-12T15:19:21+00:00');
DateTime::createFromFormat(DateTimeInterface::COOKIE, 'Monday, 14-Oct-2013 09:00:00 CEST');

DateTime::createFromFormat(DateTimeInterface::RFC822, 'Mon, 14 Oct 13 09:00:00 +0200');
DateTime::createFromFormat(DateTimeInterface::RFC850, 'Monday, 14-Oct-13 09:00:00 CEST');
DateTime::createFromFormat(DateTimeInterface::RFC1036, 'Mon, 14 Oct 13 09:00:00 +0200');
DateTime::createFromFormat(DateTimeInterface::RFC1123, 'Mon, 14 Oct 2013 09:00:00 +0200');
DateTime::createFromFormat(DateTimeInterface::RFC7231, 'Mon, 14 Oct 2013 09:00:00 GMT');
DateTime::createFromFormat(DateTimeInterface::RFC2822, 'Mon, 14 Oct 2013 09:00:00 +0200');
DateTime::createFromFormat(DateTimeInterface::RFC3339, '2013-10-14T09:00:00+02:00');
DateTime::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, '2013-10-14T09:00:00.000+02:00');

DateTime::createFromFormat(DateTimeInterface::RSS, 'Mon, 14 Oct 2013 09:00:00 +0200');
DateTime::createFromFormat(DateTimeInterface::W3C, '2013-10-14T09:00:00+02:00');

?>

Note: the format supplied to DateTime::createFromFormat() differs from the format described in DateTime::format().
Take a look at the examples below:

<?php

$iso8601date
= (new DateTime('2013-10-14'))->format('c'); // string(25) "2013-10-14T00:00:00+00:00"
$dateTimeA = DateTime::createFromFormat('c', $iso8601date); // bool(false)
$dateTimeB = DateTime::createFromFormat(DateTimeInterface::ISO8601, $iso8601date); // object(DateTime)

?>

Thus, the format rules for DateTime::createFromFormat() can be found at DateTimeImmutable::createFromFormat(). 
Link: https://www.php.net/manual/en/datetimeimmutable.createfromformat.php
To Top