How to create a string equivalent to yyyyMMddHHmmssffff in PHP?
P粉238355860
2023-07-31 12:10:17
<p>I'm trying to integrate with some VB code that uses DateTime.Now.ToString("yyyyMMddHHmmssffff") to generate a string. </p>
<p><code>DateTime.Now.ToString("yyyyMMddHHmmssffff")</code></p>
<p>The closest I can get to this in PHP is: </p>
<p><code>date('YmdHis')</code></p>
<p>But this is missing the millisecond part. </p><p>I have tried using microtime, for example: </p><p><code></code></p>
<p><code>date('YmdHis',time()).substr(str_replace(".","",microtime(true)),10,17)</code></p>
<p>But sometimes this results in a shorter string. </p>
date
The function always returns zero for microseconds. You can use the DateTime->format function and remove the last two digits to retain 4 decimal places.