Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the ISO 8601 with seconds.decimal-fraction-of-second date in php?

Tags:

php

datetime

I echo this :

  php> echo date("Y-m-d\TH:i:s");
      2011-05-27T11:21:23

How can do with date function to get this date format:

2011-01-12T14:41:35.7042252+01:00 (for example)

35.7042252 => seconds.decimal-fraction-of-second

I have tried:

php> function getTimestamp()
 ... {
 ...         return date("Y-m-d\TH:i:s") . substr((string)microtime(), 1, 8);
 ... }

php> echo getTimestamp();
2011-05-27T15:34:35.6688370 // missing +01:00 how can I do?
like image 758
newbie Avatar asked May 27 '11 09:05

newbie


People also ask

How do I format a date in ISO 8601?

ISO 8601 represents date and time by starting with the year, followed by the month, the day, the hour, the minutes, seconds and milliseconds. For example, 2020-07-10 15:00:00.000, represents the 10th of July 2020 at 3 p.m. (in local time as there is no time zone offset specified—more on that below).

What time zone is ISO 8601?

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


1 Answers

date('Y-m-d\TH:i:s.uP')

u for microseconds was added in PHP 5.2.2. For earlier or (still) broken versions (see comments):

date('Y-m-d\TH:i:s') . substr(microtime(), 1, 8) . date('P')

Or, to avoid two calls to date:

date(sprintf('Y-m-d\TH:i:s%sP', substr(microtime(), 1, 8)))
like image 157
deceze Avatar answered Sep 27 '22 15:09

deceze