Example 1 — Reading Custom Date Formats
Describe the layout of each string with format letters. PHP then reads the parts in the right order.
<?php
$d1 = date_create_from_format('d/m/Y H:i', '15/01/2024 10:30');
echo date_format($d1, 'Y-m-d H:i'), PHP_EOL;
$d2 = date_create_from_format('j-M-Y', '5-Mar-2024');
echo date_format($d2, 'Y-m-d'), PHP_EOL;
$d3 = date_create_from_format('Y-m-d H:i:s', '2024-01-15 10:30:00');
echo date_format($d3, 'l jS F'), PHP_EOL; How It Works
- The format d/m/Y H:i tells PHP that the string is day, month, year, then hour and minute.
- j-M-Y reads a day without a leading zero, a short month name such as Mar, and a year.
- Every DateTime created this way can be printed again with date_format().
- The last call prints the weekday and the day with the ordinal suffix th.

