76

I need to display and handle UTC dates in the following format:

2013-06-28T22:15:00Z

As this format is part of the ISO8601 standard I have no trouble creating DateTime objects from strings like the one above. However I can't find a clean way (meaning no string manipulations like substr and replace, etc.) to present my DateTime object in the desired format. I tried to tweak the server and php datetime settings, with little success. I always get:

$date->format(DateTime::ISO8601); // gives 2013-06-28T22:15:00+00:00

Is there any date format or configuration setting that will give me the desired string? Or I'll have to append the 'Z' manually to a custom time format?

sallaigy
  • 1,524
  • 1
  • 13
  • 12
  • 2
    +1 for using DateTime in the first place. – Herbert Jun 30 '13 at 13:30
  • Note that the accepted answer simply puts a "Z" on the end of the format string, but that does not give you the UTC date. This is likely to cause problems in many use cases where a GMT/UTC time is expected. The second answer using gmdate() is a better fit for these cases. – xtempore Jul 07 '19 at 03:48

6 Answers6

128

No, there is no special constant for the desired format. I would use:

$date->format('Y-m-d\TH:i:s\Z');

But you will have to make sure that the times you are using are really UTC to avoid interpretation errors in your application.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • This is a great answer, but would you recommend using `setTimeZone` to adjust to UTC? Or is there a cleaner way? – Matt Johnson-Pint Jun 27 '14 at 03:26
  • 2
    @MattJohnson It depends. If the timestrings are meant to be interpreted as UTC (but without containing the offset literally) and your server has a different timezone than UTC, `setTimeZone()` would change the literal value of the timestring. This might not being what you want. On the other hand, if the existing timestamps are meant to be interpreted in server's time and you *want* to transform them to UTC times, then `setTimeZone()` is required. – hek2mgl Jun 28 '14 at 08:45
  • 1
    @MattJohnson I've just come across this question and can confirm that even if the timezone is already UTC, it still comes out as `+00:00` – Dezza Sep 20 '16 at 15:19
18

In PHP 8 the format character p was added:

$timestamp = new DateTimeImmutable('2013-06-28T22:15:00Z');
echo $timestamp->format('Y-m-d\TH:i:sp');
// 2013-06-28T22:15:00Z
AndreKR
  • 32,613
  • 18
  • 106
  • 168
17

If you are using Carbon then the method is:

echo $dt->toIso8601ZuluString();    
// 2019-02-01T03:45:27Z
Edmund Sulzanok
  • 1,883
  • 3
  • 20
  • 39
  • 6
    I required the parameter `millisecond` to pass validation `echo $dt->toIso8601ZuluString('millisecond'); // 2020-11-30T10:12:00.000Z` – Luke Snowden Nov 17 '20 at 12:51
9

In order to get the UTC date in the desired format, you can use something like this:

gmdate('Y-m-d\TH:i:s\Z', $date->format('U'));
Ynhockey
  • 3,845
  • 5
  • 33
  • 51
6

To do this with the object-oriented style date object you need to first set the timezone to UTC, and then output the date:

function dateTo8601Zulu(\DateTimeInterface $date):string {
  return (clone $date)
    ->setTimezone(new \DateTimeZone('UTC'))
    ->format('Y-m-d\TH:i:s\Z');
}

Edit: clone object before changing timezone.

Chris Seufert
  • 819
  • 1
  • 7
  • 17
  • 3
    If you use **DateTime class** you may want to clone the object before setting timezone to prevent changing the original object. – holmis83 Nov 04 '19 at 20:19
1

Since PHP 7.2 DateTimeInterface::ATOM was introduced in favor of DateTimeInterface::ISO8601, although it still lives on for backward compatability reasons.

Usage

$dateTimeObject->format(DateTimeInterface::ATOM)
Michel Rummens
  • 459
  • 3
  • 9