18

I get a response from a other server with a date time like below,

"2017-01-10T18:00:00.000Z"

I want to convert this to standard date time like this, how do i do this ?

"2017-01-10 18:00:00"

is there a standard way of doing this ? or do i have rely on the regular expressions to the decode it ?

NOTE:

most people suggest i use below function

echo date("Y-m-d H:i:s", strtotime("2017-01-10T18:00:00.000Z")) . "\n";**

but output i get is wrong from this function is "2017-01-11 05:00:00" not as "2017-01-11 18:00:00" why ? I want as i said "2017-01-10 18:00:00"

mahen3d
  • 7,047
  • 13
  • 51
  • 103

3 Answers3

51

you can use date('Y-m-d h:i:s', strtotime($yourDate));
Hope it will help you :)

itoctopus
  • 4,133
  • 4
  • 32
  • 44
Amit Sahu
  • 896
  • 6
  • 7
6

Ah OK...in php, that long timestamp ending with Z can be converted to a unix timestamp with strototime and then back to a date using the date function

echo date("Y-m-d H:i:s", strtotime("2017-01-10T18:00:00.000Z")) . "\n";

WARNING: the current time zone setting on your server may result in the time being adjusted

S. Imp
  • 2,833
  • 11
  • 24
  • works fine on my Ubuntu workstation: php -r 'echo date("Y-m-d H:i:s", strtotime("2017-01-10T18:00:00.000Z")) . "\n";' 2017-01-10 10:00:00 -- although it appears to assume the original time is UTC and adjusts for my timezone. – S. Imp Jan 10 '17 at 05:02
  • 2017-01-11 05:00:00 is the output i get which is wrong 18:00 is expected – mahen3d Jan 10 '17 at 05:05
  • If I'm not mistaken, that final "Z" in your first datetime is a Timezone Designator denoting UTC time. When you use the date function, it converts the UTC time into your local timezone which accounts for the two times being different. In other words, 2017-01-10T18:00:00.000Z means UTC time and when it gets converted using the date function, the result is converted to your PHP server's local timezone. – S. Imp Jan 10 '17 at 05:14
  • Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z"). See https://www.w3.org/TR/NOTE-datetime – S. Imp Jan 10 '17 at 05:54
1

You can use the date_time() function from php.

You should take a look on this link for solving your problem.

PHP Convert ISO date to more readable format?

Community
  • 1
  • 1
Marprin
  • 186
  • 1
  • 6