DateTimeImmutable::createFromFormat() parses input according to a format you define. It returns false when parsing fails, so check the result before calling format().
$date = DateTimeImmutable::createFromFormat('d-m-Y', '14-09-2026');
if ($date === false) {
$errors = DateTimeImmutable::getLastErrors();
print_r($errors);
} else {
echo $date->format('Y-m-d');
}For validation, also inspect getLastErrors(); PHP 8.2+ may return false when there are no warnings or errors.
$date = DateTime::createFromFormat('d.n.Y', '06.1.2017');
echo $date->format('Y-M-d'); // Output is 2017-Jan-06
Procedural style
$date = date_create_from_format('Y/F/j', '2017/April/6');
echo date_format($date, 'Y-m-d'); // Output is 2017-04-06
Using Time formats
$date = DateTime::createFromFormat('d/m/Y H:i:s', '06/12/2017 23:34:59');
echo $date->format('h:i:s A'); // Output is 11:34:59 PM
$date = date_create_from_format('Y-m-d H:i:s', '2023-09-25 12:45:00');
echo date_format($date, 'Y-m-d H:i:s'); // Output: 2023-09-25 12:45:00
$date = date_create_from_format('Y-m-d', '2023-13-45');
if (!$date) {
echo "Invalid date format!";
}
$date1 = date_create_from_format('Y-m-d', '2023-09-25');
$date2 = date_create_from_format('Y-m-d', '2022-09-25');
echo $date1 > $date2 ? 'Date 1 is later' : 'Date 2 is later'; // Output: Date 1 is later
| Type | Ch | DESCRIPTION |
|---|---|---|
| Date | d | day of the month, 2 digits with leading zeros; i.e. '01' to '31' |
| j | day of the month without leading zeros; i.e. '1' to '31' | |
| Day | D | day of the week, textual, 3 letters; i.e. 'Fri' |
| l | day of the week, textual, long; i.e. 'Saturday' | |
| w | day of the week, numeric, i.e. '0' (Sunday) to '6' (Saturday) | |
| z | day of the year; i.e. '0' to '365' | |
| r | RFC 822 formatted date; i.e. 'Thu, 21 Dec 2000 16:01:07 +0200' (added in PHP 4.0.4) | |
| S | Suffix for day of the month in 2 chrs ( with j ) | |
| z | Day of the year ( from 0 to 365 ) | |
| Month | m | month; With leading zeros; i.e. '01' to '12' |
| n | month without leading zeros; i.e. '1' to '12' | |
| M | month, textual, 3 letters; i.e. 'Jan' | |
| F | month, textual, long; i.e. 'January' | |
| t | Number of days in the month ( 28 to 31 ) | |
| Year | L | 1 for Leap year, 0 if not |
| o | ISO-8601 week-numbering year. | |
| Y | year, 4 digits; i.e. '2018' | |
| y | year, 2 digits; i.e. '19' | |
| Time | ||
| Hour | g | hour, 12-hour format without leading zeros; i.e. '1' to '12' |
| G | hour, 24-hour format without leading zeros; i.e. '0' to '23' | |
| h | hour, 12-hour format; i.e. '01' to '12' | |
| H | hour, 24-hour format; i.e. '00' to '23' | |
| Minutes | i | minutes; i.e. '00' to '59' |
| Seconds | s | seconds; i.e. '00' to '59' |
| a | 'am' or 'pm' | |
| A | 'AM' or 'PM' | |
| B | Swatch Internet time | |
| I | '1' if Daylight Savings Time, '0' otherwise. | |
| L | boolean for whether it is a leap year; i.e. '0' or '1' | |
| T | Timezone setting of this machine; i.e. 'MDT' | |
| U | seconds since the epoch | |
| Z | timezone offset in seconds (i.e. '-43200' to '43200'). The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. | |
| e, O, P and T | timezone identifier ( e ) , difference in Hour and minutes to GMT (O),+- Hour : Minute (P), Short name of zone ( IST) (T) | |
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.