1

For eg I have ISO date time as : 2020-03-03T11:07:41.1708478Z

Converting using strtotime function

$dateTime = date("Y-m-d H:i:s.u",strtotime('2020-03-03T11:07:41.1708478Z'));

Result : 2020-03-03 11:07:41.000000

In above result you can see milliseconds are gone.

Andreas
  • 23,610
  • 6
  • 30
  • 62
am.dev
  • 25
  • 4
  • In the manual of Date() function it says: *Microseconds (added in PHP 5.2.2). Note that date() will always generate 000000 since it takes an integer parameter, whereas DateTime::format() does support microseconds if DateTime was created with microseconds.* That means both strtotime and date is not the tool since they both work with only seconds – Andreas Mar 03 '20 at 12:32
  • Does this answer your question? [getting date format m-d-Y H:i:s.u from milliseconds](https://stackoverflow.com/questions/17909871/getting-date-format-m-d-y-his-u-from-milliseconds) – CBroe Mar 03 '20 at 12:47

1 Answers1

3

Use DateTime because strtotime and date only uses full seconds.

$date = new DateTime('2020-03-03T11:07:41.1708478Z');
echo $date->format("Y-m-d H:i:s.u"); // 2020-03-03 11:07:41.170847

https://3v4l.org/DXVj8

But since this I assume this input format is rater fixed and wont change I could recommend you to use a simple str_replace.

echo str_replace(["T","Z"], [" ",""], '2020-03-03T11:07:41.1708478Z'); // 2020-03-03 11:07:41.170847
Andreas
  • 23,610
  • 6
  • 30
  • 62
  • https://www.php.net/manual/en/datetime.format.php (PHP 5 >= 5.2.1, PHP 7) or just use my edit with str_replace – Andreas Mar 03 '20 at 12:38