Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php Convert date time like "2017-01-10T18:00:00.000Z" to standard time [duplicate]

Tags:

date

php

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"

like image 414
mahen3d Avatar asked Jan 10 '17 04:01

mahen3d


People also ask

How convert date from yyyy mm dd to dd mm yyyy format in PHP?

Change YYYY-MM-DD to DD-MM-YYYY In the below example, we have date 2019-09-15 in YYYY-MM-DD format, and we will convert this to 15-09-2019 in DD-MM-YYYY format. $orgDate = "2019-09-15"; $newDate = date("d-m-Y", strtotime($orgDate)); echo "New date format is: ".

What is Z in date format PHP?

Z - Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400) c - The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00) r - The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)

What is T and Z in timestamp?

The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC).

What does Strtotime mean in PHP?

Definition and Usage The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


2 Answers

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

like image 125
Amit Sahu Avatar answered Oct 05 '22 11:10

Amit Sahu


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

like image 36
S. Imp Avatar answered Oct 05 '22 10:10

S. Imp