In PHP, date_create_immutable_from_format() builds a DateTimeImmutable object from a date string that follows a format you describe. If the string does not match the format, it returns false.
This is the immutable version of date_create_from_format() and the procedural alias of DateTimeImmutable::createFromFormat(). You tell PHP exactly how the text is laid out, which removes any guessing about day and month order. The returned object never changes, so it is a safe choice for modern PHP code.
Syntax
Call date_create_immutable_from_format() with the values you need. Start with this line.
This function is an alias of DateTimeImmutable::createFromFormat(). As with date_create_from_format(), any field missing from the format is filled with the current time, so start the format with ! when you only need a date.
2024-07-01T12:00:00+01:00 BST
2024-07-01T11:00:00+00:00 UTC
12:00
How It Works
London uses British Summer Time in July, so the offset is +01:00 and the abbreviation is BST.
setTimezone() on an immutable object returns a new object in the UTC timezone.
The same moment shows as 11:00 in UTC, one hour earlier than London.
The original $d is not changed and still prints 12:00.
Frequently Asked Questions
It reads a date string using a format you specify and returns a DateTimeImmutable object. If the string does not match the format, it returns false.
Yes. date_create_immutable_from_format() is the procedural alias of the static method DateTimeImmutable::createFromFormat(). They behave the same way.
date_create_from_format() returns a mutable DateTime, while this function returns a DateTimeImmutable. Everything else about the format letters and parsing is the same.
Any field not listed in the format is filled from the current moment. Put ! at the start of the format, or | at the end, to reset those fields to the epoch defaults.
The function returns false. You can call date_get_last_errors() to see warnings and error messages about the last parse.