Skip to content
Advertisement

Difference with microseconds precision between two DateTime in PHP

I can get a datetime with microseconds in PHP with a workaround as:

JavaScript

I need the difference with microseconds between two datetimes, can’t get a workaround for:

JavaScript

DateInterval::format doesn’t has the format character %u or equivalent for microseconds.

Anyone knows a workaround for this?

Advertisement

Answer

Manually creating a DateTime object with micro seconds:

JavaScript

Getting a DateTime object of the current time with microseconds:

JavaScript

Difference between two DateTime objects in microseconds (e.g. returns: 2.218939)

JavaScript

Essentially it finds the difference for the DateTime Objects using strtotime and then adding the extra microseconds on.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement