Formatos de fecha y hora admitidos

Tabla de contenidos

Esta sección describe todos los formatos diferentes que entiende el analizador de strtotime(), de DateTime y de date_create(). Los formatos están agrupados por secciones. En la mayoría de los casos, se pueden emplear los formatos de secciones diferentes en la misma cadena de fecha/hora. Se proporcionan ejemplos para cada uno de los formatos admitidos, así como una descripción del formato. En los formatos, los caracteres entre comillas simples son insensibles a mayúsculas-minúsculas, ('t' podría ser t o T), y los caracteres entre comillas dobles son sensibles a mayúsculas-minúsculas ("T" es solamente 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