Peter Griffin
Peter Griffin

Reputation: 300

PHP DateTime::createFromFormat() returns wrong year

I am trying to convert a datetime in a specific format to another format but for some reason the year is 4 years in the future

$date = DateTime::createFromFormat('Y-m-d\TH:m:i.u\Z', '2015-08-30T07:56:28.000Z')->format('Y-m-d');

this echoes out string(10) "2019-08-30"

I have tried this and it outputs correctly

$date = new DateTime();
echo $date->format('Y-m-d');

What could be causing the error with the year?

Upvotes: 1

Views: 1483

Answers (1)

ADyson
ADyson

Reputation: 61784

$date = DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', '2015-08-30T07:56:28.000Z')->format('Y-m-d');

i is used for minutes, and s for seconds. m is for months. Therefore your string is being interpreted incorrectly.

For the correct format characters to use, see http://php.net/manual/en/datetime.createfromformat.php

Upvotes: 6

Related Questions