1

When I do:

echo(strtotime('2020-06-16T08:08:18.339Z'));

strtotime gives me:

1592294898

Is there a function to convert date/time in ISO8601 format without dropping microseconds? (To a fractional number of seconds, similar to the one returned by microtime(true) function.)

PS. I'm using PHP 7.4

c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • 1
    To clarify, `strtotime` by definition returns an integer of seconds, necessarily not having the resolution to preserve microseconds. You’re looking for an alternative parser that uses floats and/or milli/microsecond resolution? – deceze Jun 17 '20 at 06:29
  • @deceze sorry, yes, clarified in the answer itself. – c00000fd Jun 17 '20 at 06:30
  • 2
    Does this answer your question? [DateTime with microseconds](https://stackoverflow.com/questions/33691428/datetime-with-microseconds) – Phil Jun 17 '20 at 06:54
  • @Phil: No. If you read what I'm asking here, I need something that does a reverse of what you found there. – c00000fd Jun 17 '20 at 06:56
  • Yeah, sorry about the insta-close. Didn't mean that. Re-opened now – Phil Jun 17 '20 at 06:57
  • What sort of result are you wanting here? A string, integer or float? – Phil Jun 17 '20 at 06:58
  • 1
    @Phil: `1592294898.339` – c00000fd Jun 17 '20 at 06:59

1 Answers1

2

As always with dates in PHP, DateTime to the rescue

$str = '2020-06-16T08:08:18.339Z';

$dt = new DateTime($str); // or DateTime::createFromFormat('Y-m-d\TH:i:s.uO', $str);

echo (float) $dt->format('U.u');

Demo ~ https://3v4l.org/ee9MZ


You could also use the DATE_RFC3339_EXTENDED constant in createFromFormat if you're only interested in milliseconds but your question says "microseconds".

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Hmm. Interesting. Do I need to explicitly specify the format? I'd prefer not to. But if I was doing `DateTime::createFromFormat(DateTime::ISO8601,'2020-06-16T08:08:18.339Z');` it was always failing. – c00000fd Jun 17 '20 at 07:13
  • 1
    @c00000fd seems not. I've update my answer to just use the `DateTime` constructor. As for why `DateTime::ISO8601` doesn't work, see https://www.php.net/manual/en/class.datetime.php#datetime.synopsis (hint, the answer is severe short-sightedness) – Phil Jun 17 '20 at 07:16
  • If you were asking about using `format('U.u')` then yes, that is required. – Phil Jun 17 '20 at 07:17
  • 1
    Thanks, dude. And no, I was asking about the first parameter for the `DateTime::createFromFormat` function. So now it works the way you showed it. Thanls. I'm assumung that `DateTime` constructor is assuming an input string to be in ISO8601 format. Right? And as for the "severe short-sightedness" - it seems to be a middle name for PHP itself. – c00000fd Jun 17 '20 at 07:22
  • Also a quick follow-up. Sorry for asking, but I'm a low-level C/C++ guy. That cast to `(float)` doesn't lose precision to a 32-bit floating point number, does it? Shouldn't it be `double` instead. – c00000fd Jun 17 '20 at 07:24
  • @c00000fd I think all PHP float type are the same but I've also been out of the PHP game for like 10 years now so I could be wrong. Plenty of reading material here ~ https://www.php.net/manual/en/language.types.float.php – Phil Jun 17 '20 at 07:26
  • OK. Well, thanks anyway. I'll see if I can find it there. – c00000fd Jun 17 '20 at 07:27