Home >Backend Development >PHP Tutorial >How to Get Date Format m-d-Y H:i:s.u from Milliseconds in PHP?
Imagine you want a formatted date, complete with microseconds, from a UNIX timestamp measured in milliseconds. But you repeatedly end up with 000000 and a result like this:
07-28-2013 11:26:14.000000
The solution involves using the input format U.u:
$now = DateTime::createFromFormat('U.u', microtime(true)); echo $now->format("m-d-Y H:i:s.u");
This gives the following output:
04-13-2015 05:56:22.082300
From the PHP manual for date formats, we have:
Normally, createFromFormat() uses the local time zone if one is not specified. However, microtime() returns elapsed seconds since the Unix Epoch in UTC, so the DateTime object is implicitly initialized to UTC. This is fine for server-internal tasks, but if you need to display the time for a specific time zone, set the time zone accordingly after initializing the object. Use the setTimeZone() method for this purpose.
The above is the detailed content of How to Get Date Format m-d-Y H:i:s.u from Milliseconds in PHP?. For more information, please follow other related articles on the PHP Chinese website!