search

Home  >  Q&A  >  body text

How to create a string equivalent to yyyyMMddHHmmssffff in PHP?

<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>
P粉238355860P粉238355860530 days ago536

reply all(1)I'll reply

  • P粉978551081

    P粉9785510812023-08-01 10:21:59

    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.

    substr((new DateTime()) -> format('YmdHisu'), 0, 18);

    reply
    0
  • Cancelreply