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_formatParses a time string according to a specified format

Description

Object-oriented style

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

Procedural style

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

Returns a new DateTime object representing the date and time specified by the datetime string, which was formatted in the given format.

Like DateTimeImmutable::createFromFormat() and date_create_immutable_from_format(), respectively, but creates a DateTime object.

Return Values

Returns a new DateTime instance or false on failure.

See Also

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