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_formatWertet eine Zeitangabe gemäß dem angegebenen Format aus

Beschreibung

Objektorientierter Stil

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

Prozeduraler Stil

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

Gibt ein neues DateTime-Objekt zurück. Es stellt das Datum und die Uhrzeit dar, die in der Zeichenkette datetime angegeben sind und gemäß dem angegebenen format formatiert wurden.

Wie DateTimeImmutable::createFromFormat() bzw. date_create_immutable_from_format(), erzeugt aber ein DateTime-Objekt.

Parameter-Liste

Siehe DateTimeImmutable::createFromFormat.

Rückgabewerte

Gibt eine neue DateTime-Instanz zurück. Bei einem Fehler wird false zurückgegeben.

Siehe auch

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