Convert date time from zulu format: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
mNo edit summary
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
Convert date time from T Z format (ISO 8601 format, Z(ulu) time) e.g. {{kbd |key=2020-05-08T10:01:45Z}} or {{kbd |key=2020-05-06T22:48:51.660Z}}
Convert date time from T Z format (ISO 8601 format<ref>[https://en.wikipedia.org/wiki/ISO_8601 ISO 8601 - Wikipedia]</ref>; Zulu time format<ref>[https://greenwichmeantime.com/articles/history/zulu/ Zulu Time | GMT]</ref>) e.g. {{kbd |key=2020-05-08T10:01:45Z}} or {{kbd |key=2020-05-06T22:48:51.660Z}}


== PHP approach ==
== PHP approach ==
Line 36: Line 36:


== References ==
== References ==
* [https://en.wikipedia.org/wiki/ISO_8601 ISO 8601 - Wikipedia]
<references />
 
 
* [https://stackoverflow.com/questions/15902464/format-of-2013-04-09t100000z-in-php Format of 2013-04-09T10:00:00Z in php - Stack Overflow]
* [https://stackoverflow.com/questions/15902464/format-of-2013-04-09t100000z-in-php Format of 2013-04-09T10:00:00Z in php - Stack Overflow]
* [https://stackoverflow.com/questions/44802061/convert-string-to-datetime-in-my-sql mysql - convert string to datetime in my sql - Stack Overflow]
* [https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_str-to-date STR_TO_DATE(str,format)]
* [https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_str-to-date STR_TO_DATE(str,format)]
* [https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format DATE_FORMAT(date,format)]
* [https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format DATE_FORMAT(date,format)]

Revision as of 18:59, 8 May 2020

Convert date time from T Z format (ISO 8601 format[1]; Zulu time format[2]) e.g. 2020-05-08T10:01:45Z or 2020-05-06T22:48:51.660Z

PHP approach

Coordinated Universal Time (UTC)

echo date('Y-m-d H:i:s', strtotime('2020-05-08T09:59:08Z')) . PHP_EOL;
// returns 2020-05-08 09:59:08

Icon_exclaim.gif The returned result is server timezone dependent if you set the timezone using date_default_timezone_set.

date_default_timezone_set("Asia/Taipei");
echo date('Y-m-d H:i:s', strtotime('2020-05-08T09:59:08Z')) . PHP_EOL;
// returns 2020-05-08 17:59:08 

MySQL approach

Coordinated Universal Time (UTC)

-- case1: end with decimal & Z
SET @myTime := '2020-05-06T22:48:51.660Z';
SELECT str_to_date(@myTime, '%Y-%m-%dT%H:%i:%s.%fZ');
-- returns 2020-05-06 22:48:51.660000


-- case2: not end with decimal & Z
SET @myTime := '2020-05-08T09:59:08Z';
SELECT str_to_date(@myTime, '%Y-%m-%dT%H:%i:%sZ');
-- returns 2020-05-08 09:59:08

SET @myTime := '2020-05-08T09:59:08Z';
SELECT str_to_date(@myTime, '%Y-%m-%dT%H:%i:%s.%fZ');
-- returns null


References