日期时间ISO 8601

日期时间ISO 8601

问题描述:

Working in a project find a problem. If see ISO 8601, the following is a valid date-time representation: 1997-09-02T10 (representing 09 September 1997, 10:00). BUT:

$tz = new DateTimeZone("Europe/Amsterdam");

$dateObject = new DateTime( '2012-02-02T10', $tz );

echo $dateObject->format( 'j F Y H:i' );

Give me an error: Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string ('2012-02-02T10').

What is this??

You input format is not valid. See supported input date and time formats.

Try this instead:

$tz = new DateTimeZone("Europe/Amsterdam");
$dateObject = DateTime::createFromFormat('!Y-m-d\TH',  '2012-02-02T10', $tz);
echo $dateObject->format('j F Y H:i');

Demo.