开发者

Converting MYSQL timestamp to ISO-8601 without timzone offset

开发者 https://www.devze.com 2023-03-20 00:42 出处:网络
I\'m trying to use timeago( http://timeago.yarp.com/ ) and have found solutions for converting timestamps from MYSQL using php to ISO-8601.

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 offs开发者_JAVA技巧et 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?


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.


The 'c' will return the entire date, including the timezone offset. You will have to build it manually using the other options. Try this:

$time_stamp = time();
echo date('o-m-N',$time_stamp)."T".date('H:i:s',$time_stamp)."Z";


http://php.net/manual/en/class.datetime.php#datetime.constants.types

const string ISO8601 = "Y-m-d\TH:i:sO";

You can use your own format:

date('Y-m-d\TH:i:s\Z',strtotime($TimeStamp));


Simple for Open Graph

 date('Y-m-d\TH:i', strtotime('2015/09/24 08:46:50))


Another way:

substr(date('c',strtotime($Timestamp)),0,-6).'Z';

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号