Is there a PHP7 equivalent to the C#.NET DateTime.Now.ToString(“yyyyMMddHHmmssfff”)? [closed]

北城以北 提交于 2021-02-10 05:59:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!