Converting MYSQL timestamp to ISO-8601 without timzone offset

Jon Schatz picture Jon Schatz · Jul 11, 2011 · Viewed 7k times · Source

I'm trying to use timeago ( http://timeago.yarp.com/ ) and have found solutions for converting timestamps from MYSQL using php to ISO-8601.

date('c',strtotime($TimeStamp));

This works fine except im getting the timezone offset at the end

2011-07-10T08:46:50-**05:00**

what I want is 2011-07-10T08:46:50Z

Does anyone have a solution or know why i'm getting the timezone offset?

Answer

Marc B picture Marc B · Jul 11, 2011

You can do it directly in MySQL:

SELECT DATE_FORMAT(yourfield, '%Y-%m-%dT%H:%i:%s0Z')

There is a GET_FORMAT(datetime, 'iso') call as well, but that returns the format string for ISO 9075, which is not quite what you want. Since it doesn't do 8601 directly, you have to build the format string yourself.