サポートする日付と時刻の書式

目次

この節では DateTimeImmutable, DateTime, date_create(), date_create_immutable(), および strtotime() が解釈可能な全ての書式について説明します。 これらの書式はセクションごとにグループ分けされています。 たいていの場合、一つの日付・時刻文字列の中でホワイトスペース、 コンマまたはドットで区切られた異なるセクションの書式を併用することができます。 それぞれの書式について、説明と一緒に1個以上の例を示してあります。 シングルクォートで囲まれたフォーマットは大文字小文字を同一視します ('t't とも T とも書けます)。 ダブルクォートで囲まれたフォーマットは大文字小文字を同一視しません ("T"T としか書けません)。

add a note add a note

User Contributed Notes 1 note

up
27
Ray.Paseur sometimes uses Gmail
7 years ago
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
To Top