Datetime with Z at the end cover image
Photo by Alexey Savchenko

Datetime with Z at the end

Working with external API's I often encounter timestamps like these: 2021-11-15T23:15:35Z.

Each time I have to open the Carbon documentation and read through the entire thing with a magnifying glass to look for the format that ends with a Z.

After scanning through the carbon documentation I found the correct entry:

1echo $dt->toIso8601ZuluString(); // 2019-02-01T03:45:27Z

Etymology

In an effort to remember the format better I investigated why it's called a Zulu string, and I found a stack exchange post that explains that the Z indicates UTC Zero hours. In the NATO phonetic alphabet Z is Zulu so that's why it's called Zulu time.

Code example

Here's a laravel example for good measure:

1$tz = new DateTimeZone('Europe/Oslo');
2echo (new \Illuminate\Support\Carbon('2021-11-15 23:15:35', $tz))
3 ->toIso8601ZuluString(); // 2021-11-15T22:15:35Z

Written by Eivin Landa • November 15, 2021 • Updated March 24, 2022