Php iso 8601 without timezone

My timestamp is like this:

$timestamp = "2018-08-28T08:49:44+00:00";

Easy to convert this to a unix timestamp:

$unixtime = strtotime($timestamp);
echo $unixtime; // result: 1535446184

My desired result is to get the timestamp with timezone for my current timezone. It should be like this:

$timestamp_with_timezone = "2018-08-28T10:49:44+02:00";

However if I do:

echo date('c',$unixtime);

The result is again:

2018-08-28T08:49:44+00:00

What is the correct way to get the desired local time date in ISO 8601 format?

其他參考

  • ISO 8601 in Wiki
  • Date and Time Formats in W3C

The formats are as follows. Exactly the components shown here must be present, with exactly this punctuation. Note that the "T" appears literally in the string, to indicate the beginning of the time element, as specified in ISO 8601.

   Year:
      YYYY (eg 1997)
   Year and month:
      YYYY-MM (eg 1997-07)
   Complete date:
      YYYY-MM-DD (eg 1997-07-16)
   Complete date plus hours and minutes:
      YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
   Complete date plus hours, minutes and seconds:
      YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
   Complete date plus hours, minutes, seconds and a decimal fraction of a second
      YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)

where:

     YYYY = four-digit year
     MM   = two-digit month (01=January, etc.)
     DD   = two-digit day of month (01 through 31)
     hh   = two digits of hour (00 through 23) (am/pm NOT allowed)
     mm   = two digits of minute (00 through 59)
     ss   = two digits of second (00 through 59)
     s    = one or more digits representing a decimal fraction of a second
     TZD  = time zone designator (Z or +hh:mm or -hh:mm)

  • PHP.Watch
  • Versions
  • 8.0

There is a new date format introduced in PHP 8.0: p. This new lower case "p" date format behaves similar to the upper case P, which shows the time zone offset. The difference between P and p format is that the new p format uses Z for UTC time, while the P format uses +00:00.

The ISO 8601 date format permits UTC time with +00:00 format, or with Z. This means both date formats below are valid:

  • 2020-09-09T20:42:34+00:00
  • 2020-09-09T20:42:34Z

With the new p date formatter, it is now possible to create a date with the second pattern above as well:

date('Y-m-d\TH:i:sp');

Here is a quick list of PHP ISO8601-related date formats:

<PHP 8.0>=PHP 8.0
DateTimeInterface::ATOM
DATE_ATOM
Valid ISO 8601 date format.
2020-09-09T20:42:34+00:00 2020-09-09T20:42:34+00:00
c
Valid ISO 8601 date format.
2020-09-09T20:42:34+00:00 2020-09-09T20:42:34+00:00
P +00:00 +00:00
p
p is new in PHP 8.0
p Z
Y-m-d\TH:i:sP
Valid ISO 8601 date format. Same as c and DATE_ATOM
2020-09-09T20:42:34+00:00 2020-09-09T20:42:34+00:00
Y-m-d\TH:i:sp
p is new in PHP 8.0
2020-09-09T20:42:34p 2020-09-09T20:42:34+00:00

Dates formatted with +00:00 to indicate UTC times are perfectly valid. Quoting from Wikipedia:

An offset of zero, in addition to having the special representation "Z", can also be stated numerically as "+00:00", "+0000", or "+00".

Backwards Compatibility Impact

UTC offsets indicated with +00:00 is perfectly valid ISO 8601 date formats. If it is absolutely necessary to use Z instead of of +00:00 for UTC time, the new p format can be helpful.

While it is not possible to backport the formatter itself, a simple string-replace hack could achieve the same results as PHP 8.0 in all PHP versions since 5:

str_replace('+00:00', 'Z', date('c'));

Implementation

The DateTime class

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

Introduction

Representation of date and time.

This class behaves the same as DateTimeImmutable except objects are modified itself when modification methods such as DateTime::modify() are called.

Warning

Calling methods on objects of the class DateTime will change the information encapsulated in these objects, if you want to prevent that you will have to use clone operator to create a new object. Use DateTimeImmutable instead of DateTime to obtain this recommended behaviour by default.

Class synopsis

public setTime(
    int $hour,
    int $minute,
    int $second = 0,
    int $microsecond = 0
): DateTime

public format(string $format): string

}

Changelog

VersionDescription
7.2.0 The class constants of DateTime are now defined on DateTimeInterface.
7.1.0 The DateTime constructor now includes the current microseconds in the constructed value. Before this, it would always initialise the microseconds to 0.

Table of Contents

  • DateTime::add — Modifies a DateTime object, with added amount of days, months, years, hours, minutes and seconds
  • DateTime::__construct — Returns new DateTime object
  • DateTime::createFromFormat — Parses a time string according to a specified format
  • DateTime::createFromImmutable — Returns new DateTime object encapsulating the given DateTimeImmutable object
  • DateTime::createFromInterface — Returns new DateTime object encapsulating the given DateTimeInterface object
  • DateTime::getLastErrors — Returns the warnings and errors
  • DateTime::modify — Alters the timestamp
  • DateTime::__set_state — The __set_state handler
  • DateTime::setDate — Sets the date
  • DateTime::setISODate — Sets the ISO date
  • DateTime::setTime — Sets the time
  • DateTime::setTimestamp — Sets the date and time based on an Unix timestamp
  • DateTime::setTimezone — Sets the time zone for the DateTime object
  • DateTime::sub — Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object

There are no user contributed notes for this page.

Does ISO 8601 include timezone?

Time zones in ISO 8601 are represented as local time (with the location unspecified), as UTC, or as an offset from UTC.

Is ISO 8601 always UTC?

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss. sssZ , respectively). The timezone is always zero UTC offset, as denoted by the suffix Z .

How do I convert DateTime to ISO 8601 in PHP?

“php convert date to iso 8601” Code Answer.
$datetime = new DateTime('2010-12-30 23:21:46');.
echo $datetime->format(DateTime::ATOM); // Updated ISO8601..
echo date(DATE_ISO8601, strtotime('2010-12-30 23:21:46'));.

Is ISO 8601 the same as UTC?

They're for different purposes. UTC is the primary time standard by which the world regulates clocks and time. ISO is standard format time.