Supported Date and Time Formats

Table of Contents

This section describes all the different formats that the DateTimeImmutable, DateTime, date_create(), date_create_immutable(), and strtotime() parser understands. The formats are grouped by section. In most cases formats from different sections, separated by whitespace, comma or dot, can be used in the same date/time string. For each of the supported formats, one or more examples are given, as well as a description for the format. Characters in single quotes in the formats are case-insensitive ('t' could be t or T), characters in double quotes are case-sensitive ("T" is only T).

User Contributed Notes

Ray.Paseur sometimes uses Gmail 19-Oct-2016 03:59
When you've got external inputs that do not strictly follow the formatting and disambiguation rules, you may still be able to use the static method ::createFromFormat() to create a usable DateTime object

<?php
/**
 * Date values separated by slash are assumed to be in American order: m/d/y
 * Date values separated by dash are assumed to be in European order: d-m-y
 * Exact formats for date/time strings can be injected with ::createFromFormat()
 */
error_reporting(E_ALL);

// THIS IS INVALID, WOULD IMPLY MONTH == 19
$external = "19/10/2016 14:48:21";

// HOWEVER WE CAN INJECT THE FORMATTING WHEN WE DECODE THE DATE
$format = "d/m/Y H:i:s";
$dateobj = DateTime::createFromFormat($format, $external);

$iso_datetime = $dateobj->format(Datetime::ATOM);
echo
"SUCCESS: $external EQUALS ISO-8601 $iso_datetime";

// MAN PAGE: http://php.net/manual/en/datetime.createfromformat.php
PHP8中文手册 站长在线 整理 版权归PHP文档组所有