Convert date time from zulu format: Difference between revisions
Jump to navigation
Jump to search
m
no edit summary
m (→PHP approach) |
mNo edit summary |
||
| (7 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
Convert date time from | Convert date time from Zulu format ([https://en.wikipedia.org/wiki/ISO_8601 ISO 8601]) or convert date time to Zulu format | ||
== PHP approach == | == 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={{Template:Today}}T10:01:45Z}} or {{kbd |key={{Template:Today}}T22:48:51.660Z}} | |||
=== PHP approach === | |||
Coordinated Universal Time (UTC) | Coordinated Universal Time (UTC) | ||
<pre> | <pre> | ||
| Line 15: | Line 18: | ||
</pre> | </pre> | ||
== MySQL approach == | === MySQL approach === | ||
Coordinated Universal Time (UTC) | Coordinated Universal Time (UTC) | ||
<pre> | <pre> | ||
| Line 34: | Line 37: | ||
</pre> | </pre> | ||
== Convert date time to Zulu format == | |||
=== PHP approach === | |||
<pre> | |||
// 指定時間 | |||
$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 | |||
</pre> | |||
== References == | == References == | ||
<references /> | |||
== Further readings == | |||
* [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://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_str-to-date STR_TO_DATE(str,format)] | * [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_date-format DATE_FORMAT(date,format)] | * MySQL [https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_str-to-date STR_TO_DATE(str,format)] | ||
* MySQL [https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format DATE_FORMAT(date,format)] | |||
* PHP [https://www.php.net/manual/en/function.date-default-timezone-set.php PHP: date_default_timezone_set - Manual] | |||
[[Category:PHP]] | [[Category:PHP]] | ||