10

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?
hakre
  • 193,403
  • 52
  • 435
  • 836
newbie
  • 2,412
  • 5
  • 22
  • 26

5 Answers5

13
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)))
deceze
  • 510,633
  • 85
  • 743
  • 889
  • yes but date('Y-m-d\TH:i:s.uP') have a bug echo date('Y-m-d\TH:i:s.uP');=>2011-05-27T15:55:31.000000+02:00 – newbie May 27 '11 at 13:56
  • Silly PHP. Go for `microtime` then. :) – deceze May 27 '11 at 13:58
  • 3
    Don't do this, you're generating two or three different timestamps and then combining them. Even the "avoid two calls to date" version calls both date() and microtime() separately, and those will return different system times. See below. – jab May 20 '16 at 23:34
4

Best performance:

substr_replace(date('c'), substr(microtime(), 1, 8), 19, 0);
Birla
  • 1,170
  • 11
  • 30
4

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
Community
  • 1
  • 1
jchavannes
  • 2,440
  • 1
  • 26
  • 15
  • the 'source' link references Lucky's code which contains errors. See this answer for corrections :) http://stackoverflow.com/a/38334226/614616 – hozza Jul 12 '16 at 16:55
0

As a solution for 2020 with the DateTime class, which was added in PHP 5.2, you can do a simple one liner to get the wanted format.

For example:

echo (new DateTime())->format('Y-m-d\TH:i:s.uP');
// 2020-04-23T09:18:25.311075+02:00

The DateTimeInterface, which is implemented by the DateTime class, brings its own constants for widely used date formats. If you know the format, you can use a constant for that.

echo var_dump($datetime->format(DateTime::RFC3339_EXTENDED));
// 2020-04-23T09:18:25.311+02:00

As object orientated programming is widely spread in the PHP world, this could be a possible solution, too.

Marcel
  • 4,854
  • 1
  • 14
  • 24
-1

If parsing the string returned by microtime makes you vomit in your mouth, and you don't want multiple distinct timestamps munged together into your output, you can do this:

$unow = microtime(true);
sprintf("%s.%06d%s", date("Y-m-d\TH:i:s", $unow), ($unow - floor($unow))*1e6, date("P", $unow));
jab
  • 4,053
  • 3
  • 33
  • 40
  • This fails about 10% of the time when `microtime()` is less than `0.1` since it doesn't do left-0-padding. E.g. `.0272390` will get displayed as `.272390`. – jchavannes May 22 '16 at 18:00
  • That's not a "failure" precisely, but yes. Edited to fix, trivially. – jab May 24 '16 at 21:54