Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert yyyy-MM-ddTHH:mm:ssZ to yyyy-MM-dd HH:mm:ss?

Tags:

php

timestamp

Paypal returns a timestamp of the following format:

yyyy-MM-ddTHH:mm:ssZ

And I don't quite know what to do with it...

How can I convert it to yyyy-MM-dd HH:mm:ss using my local timezone in php?

I'm tempted to preg_replace the mysterious letters, but something tells me there must a better way. There also appears to be 8 hours difference to my zone which I'm not sure how to substract.

like image 835
Anonymous Avatar asked Sep 09 '12 21:09

Anonymous


People also ask

What is SSSZ in date format?

Dates are formatted using the following format: "yyyy-MM-dd'T'hh:mm:ss'Z'" if in UTC or "yyyy-MM-dd'T'hh:mm:ss[+|-]hh:mm" otherwise. On the contrary to the time zone, by default the number of milliseconds is not displayed. However, when displayed, the format is: "yyyy-MM-dd'T'hh:mm:ss.

What is ZZZ timestamp?

Timestamps are always of the form YYYY-MM-DD hh:mm:ss ZZZ. where YYYY is the year (1970-2999), MM is the month (01-12), DD is the day (01-31), hh is the hours (00-23), mm is the minutes (00-59), ss is the seconds (00-59), and ZZZ is the timezone.

What is simple date format?

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.


2 Answers

Use DateTime class to do your magic.

$date = new DateTime('2012-09-09T21:24:34Z');
$date->format('Y-m-d'); # read format from date() function
like image 74
Glavić Avatar answered Nov 08 '22 14:11

Glavić


You can use strtotime() to get a UNIX timestamp. From there you can do whatever you need: DateTime object, date(), etc.

Example with date():

echo date('r', strtotime('2012-09-10T10:00:00Z'));
like image 24
Jason McCreary Avatar answered Nov 08 '22 12:11

Jason McCreary