php8.5
Home/ Manual/ datetime / datetime/ DateTime::createFromFormat

DateTime::createFromFormat

PHP function Edit on GitHub ✎

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

Parses a time string according to a specified format

Description

Oop

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

Procedural

date_create_from_format(string $format, string $datetime, DateTimeZone|null $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.

This method, including parameters, examples, and considerations are documented on the DateTimeImmutable::createFromFormat page.

Parameters

See DateTimeImmutable::createFromFormat.

Return Values

Returns a new DateTime instanceFalseforfailure.

Errors/Exceptions

This method throws ValueError when the datetime contains NULL-bytes.

Changelog

VersionDescription
8.0.21, 8.1.8, 8.2.0Now throws ValueError when NULL-bytes are passed into datetime, which previously was silently ignored.

Examples

For an extensive set of examples, see DateTimeImmutable::createFromFormat.

See Also

Source: reference/datetime/datetime/createfromformat.xml · from the official PHP manual (php/doc-en)