问题
Considering this C#.NET code:
DateTime.Now.ToString("yyyyMMddHHmmssfff")
, is there any equivalent in PHP?
For the moment, I use the following:
$date = new DateTime();
$timestamp = $date->getTimestamp();
$formatted_timestamp = gmdate("YmdHms", $timestamp) . round(microtime(true) * 1000);
However, it doesn't output the same results (from the seconds).
回答1:
Since you're already using a DateTime
object, you could simply format it:
$date = new DateTime();
return $date->format('YmdHisv');
the 'v'
is what you're looking for (milliseconds).
Caution: this requires PHP 7.1
, if you instantiate DateTime()
with no arg, in order to obtain some non-0 milli (or micro) seconds.
来源:https://stackoverflow.com/questions/55381248/is-there-a-php7-equivalent-to-the-c-net-datetime-now-tostringyyyymmddhhmmssff