How to convert iso date time with milliseconds to date time format in php so that milliseconds don't go away?

amit suyal :

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 :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=198745&siteId=1