0

I am trying to integrate with some VB code that generates a string using

DateTime.Now.ToString("yyyyMMddHHmmssffff")

The closest I can get to this in PHP has been

date('YmdHis')

But this lacks the milliseconds,

I have tried playing with microtime e.g.

date('YmdHis',time()).substr(str_replace(".","",microtime(true)),10,17)

But sometimes this results in a shorter string

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
user1765369
  • 1,333
  • 3
  • 11
  • 19
  • 2
    Milliseconds are shown using `v` as per the [documentation](https://www.php.net/manual/en/datetime.format.php). See the note just above the milliseconds entry though...use the proper DateTime object to get a better result – ADyson Jul 31 '23 at 18:39

1 Answers1

1

date function always returns zero for microseconds. Use DateTime -> format and chop the last two digits off so that 4 fractional digits are left.

substr((new DateTime()) -> format('YmdHisu'), 0, 18);
Stefanov.sm
  • 11,215
  • 2
  • 21
  • 21