Mohammed Kumel
Mohammed Kumel

Reputation: 151

Displaying actual time using iso 8601

I m referring to this: Converting ISO 8601 format to d M Y in PHP

I tried this same as this: echo date("d M Y H:i:s", strtotime($time));

But time is not shown as saved in database. Its showing few hours difference.

Database has: 2016-03-20T23:30:51+00:00

With above php echo i get: 21 Mar 2016 00:30:51 Where as it must be 20 Mar 2016 23:30:51

Above example shows additional of 1 hour y? I tried using this format to display hour & minute but time shown is wrong. Time display has few hours difference. Why is this so?

Upvotes: 2

Views: 1114

Answers (2)

fusion3k
fusion3k

Reputation: 11689

Your date format 2016-03-20T23:30:51+00:00 reveals a GMT DateTime (side note: the same TimeZone used by php/unix timestamps).

So when you write:

echo date( "d M Y H:i:s", strtotime( $time ) );

You obtain yet the correct date converted in your system TimeZone.

You can use DateTime class to perform more operations with dates:

$date = new DateTime( $time );
echo $date->format("d M Y H:i:s");

will print the date in original (GMT) format.

$date->setTimezone( new DateTimeZone('Europe/Berlin') );
echo $date->format("d M Y H:i:s");

will print the date in 'Europe/Berlin' Timezone.


Side note: saving dates in ISO 8601 UTC/GMT is actually the best choice.


Upvotes: 2

Xaraxia
Xaraxia

Reputation: 329

Your system is using its local timezone instead of UTC, whereas the database is in UTC. If you wish to display in UTC, you can use http://php.net/manual/en/function.date-default-timezone-set.php to set the timezone to UTC before calling strtotime and date.

Upvotes: 1

Related Questions