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

By doing separate [date] calls you have a small chance of two time stamps being out of order: eg call date at 1:29:22.999999 and mircotime at 1:29:23.000001. On my server consecutive calls are about 10 us apart.

Source

Try this instead:

list($usec, $sec) = explode(" ", microtime());
echo date("Y-m-d\TH:i:s", $sec) . substr($usec, 1, 8) . date("P", $sec);

E.g.:

2015-07-19T16:59:16.0113674-07:00

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)))

Best performance:

substr_replace(date('c'), substr(microtime(), 1, 8), 19, 0);

Tags:

Datetime

Php