Convert date time from zulu format: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
No edit summary
Line 2: Line 2:


== Convert date time from Zulu format ==
== Convert date time from Zulu format ==
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}}
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={{Template:Today}}T10:01:45Z}} or {{kbd |key={{Template:Today}}T22:48:51.660Z}}


=== PHP approach ===
=== PHP approach ===
Line 36: Line 36:
-- returns null
-- returns null
</pre>
</pre>


== Convert date time to Zulu format ==
== Convert date time to Zulu format ==

Revision as of 19:12, 26 April 2023

Convert date time from Zulu format or convert date time to Zulu format

Convert date time from Zulu format

Convert date time from T Z format (ISO 8601 format[1]; Zulu time format[2]) e.g. 2026-05-12T10:01:45Z or 2026-05-12T22: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

Convert date time to Zulu format

PHP approach

// 指定時間
$dateTimeString = "2023-04-26 19:01:23";

// 特定時區的 identifier (識別符,例如 UTC+8)
$timeZoneIdentifier = 'Asia/Taipei'; // 'Asia/Taipei' 是 UTC+8 的其中一個時區識別符

// 建立 DateTimeZone 物件,指定時區
$timeZone = new DateTimeZone($timeZoneIdentifier);

// 建立 DateTime 物件,解析日期時間字串,並設置時區
$dateTime = new DateTime($dateTimeString, $timeZone);

// 將 DateTime 物件的時區設置為 UTC
$dateTime->setTimezone(new DateTimeZone('UTC'));

// 格式化日期時間為 Zulu 時間格式並輸出
echo $dateTime->format('Y-m-d\TH:i:s.000\Z');
// 輸出:2023-04-26T11:01:23Z

References

Further readings