Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Date Format Reading

<?php

// 2006
echo (new Datetime(    '6-11-07T00:00:00'))->format('Y-m-d').PHP_EOL;
echo (new Datetime(   '06-11-07T00:00:00'))->format('Y-m-d').PHP_EOL;

// 2026
echo (new Datetime(   '26-11-07T00:00:00'))->format('Y-m-d').PHP_EOL;
echo (new Datetime(  '026-11-07T00:00:00'))->format('Y-m-d').PHP_EOL;
echo (new Datetime( '2026-11-07T00:00:00'))->format('Y-m-d').PHP_EOL;

// 9996
echo (new Datetime( '9996-11-07T00:00:00'))->format('Y-m-d').PHP_EOL;

// This drops the first 1
echo (new Datetime('12027-11-06'))->format('Y-m-d').PHP_EOL;

// This fails on error
echo (new Datetime('12026-11-07T00:00:00'))->format('Y-m-d').PHP_EOL;

Reading dates from strings is difficult, as the values may become ambiguous. For example, 01-02-03 may end up in 2001 or 2003, depending on the local formatting.

These ambiguities appear in PHP’s Datetime, as it must decide upon a format. This leads to some surprises, like this ones. Note that there are no reported error.

This tip was inspired by Marco Acorte.

See Also

PHP Features

Last updated: 09 November 2026