Difference between revisions of "Convert date time from t z format"

From LemonWiki共筆
Jump to navigation Jump to search
(Created page with "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}} == PHP approach == Coordinate...")
 
Line 8: Line 8:
 
</pre>
 
</pre>
  
{{exclaim}} The returned result is server timezone dependent if you specify the timezone
+
{{exclaim}} The returned result is server timezone dependent if you set the timezone using [https://www.php.net/manual/en/function.date-default-timezone-set.php date_default_timezone_set].
 
<pre>
 
<pre>
 
date_default_timezone_set("Asia/Taipei");
 
date_default_timezone_set("Asia/Taipei");

Revision as of 18:54, 8 May 2020

Convert date time from T Z format (ISO 8601 format, Z(ulu) time) 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