2

I am trying to generate a timestamp in PHP for use with an API. I cannot seem to get the timestamp to format as it should. The proper format is:

UTC ISO8601 DateTime format: YYYY-MM-DDTHH:MM:SS.mmmmmmmZ

Example: 2013-04-24T11:11:50.2829708Z

Edit, Let me clarify on the actual issue I am having:

The 'Z' Property is returning the timezone offset in seconds. I need the offset returned as date('c') returns it.

Example: +01:00 instead of 3600

Is there a built in function for this in PHP?

jan.wojo
  • 33
  • 1
  • 1
  • 4
  • possible duplicate of [PHP DateTime microseconds always returns 0](http://stackoverflow.com/questions/169428/php-datetime-microseconds-always-returns-0) – Matteo Tassinari Nov 12 '14 at 16:57
  • Hey Matteo, I have read through those threads pretty well and updated my original question. I dont think the issue I am trying to solve is talked about in those. – jan.wojo Nov 12 '14 at 21:16

2 Answers2

9

You are looking for the DateTime::format method in combination with the c formatter or the DateTime::ISO8601 constant:

$timestamp = new DateTime();
echo $timestamp->format('c'); // Returns ISO8601 in proper format
echo $timestamp->format(DateTime::ISO8601); // Works the same since const ISO8601 = "Y-m-d\TH:i:sO"
sjagr
  • 15,983
  • 5
  • 40
  • 67
  • 1
    you can also just use the constant: `$timestamp->format(DateTime::ISO8601);` which is equal to `'Y-m-d\TH:i:sO'` – prodigitalson Nov 12 '14 at 21:24
  • 1
    @prodigitalson Thanks, added your alternative with comments – sjagr Nov 12 '14 at 21:26
  • I ended up using something similar to get to the final format I needed: `$t = microtime(true); $micro = sprintf("%06d",($t - floor($t)) * 1000000); $timeZoneOffset = strstr(date('c', $t), '+'); $d = new DateTime( date('Y-m-d H:i:s.'.$micro,$t)); echo $d->format("Y-m-d\TH:i:s.u" . $timeZoneOffset);` – jan.wojo Nov 12 '14 at 21:35
  • Be careful because format `'c'` doesn't equal to `DateTime::ISO8601`. – Jsowa Jan 07 '23 at 18:06
3

To print date in ISO 8601 in PHP you can use the rather simple procedural style date() function like that:

$isoDate = date('c') // outputs 2017-10-18T22:44:26+00:00 'Y-m-d\TH:i:sO'  

Or if you prefer to OOP style then you can use DateTime() like this:

$date = DateTime('2010-01-01');
echo date_format($date, 'c');

The list of date format/constants that PHP provides are mentioned here:

const string ATOM = "Y-m-d\TH:i:sP" ;
const string COOKIE = "l, d-M-Y H:i:s T" ;
const string ISO8601 = "Y-m-d\TH:i:sO" ;
const string RFC822 = "D, d M y H:i:s O" ;
const string RFC850 = "l, d-M-y H:i:s T" ;
const string RFC1036 = "D, d M y H:i:s O" ;
const string RFC1123 = "D, d M Y H:i:s O" ;
const string RFC2822 = "D, d M Y H:i:s O" ;
const string RFC3339 = "Y-m-d\TH:i:sP" ;
const string RSS = "D, d M Y H:i:s O" ;
const string W3C = "Y-m-d\TH:i:sP" ;

So good thing is that we have ISO 8601 format in there. However, the value may not be same as you're expecting (YYYY-MM-DDTHH:MM:SS.mmmmmmmZ). According to ISO 8601 Wikipedia page, these are the valid formats:

2017-10-18T22:33:58+00:00
2017-10-18T22:33:58Z
20171018T223358Z

PHP probably prefers the first one. I was having somewhat similar problem dealing with dates between PHP and Javascript, because Javascript one has a trailing Z at the end. I ended up writing this to fix the issue:

$isoDate = date('Y-m-d\TH:i:s.000') . 'Z'; // outputs: 2017-10-18T23:04:17.000Z

NOTE: the reason I have 3 decimals is I noticed that Javascript date is using this format, it may not be necessary for you.

nkh
  • 5,481
  • 1
  • 15
  • 11