I have a need to get the difference between two dates in PHP, down to the millisecond. The dates are both DateTime objects (or compatible variants). Since PHP 7.2, DateTime stores milliseconds as well.
DateTime::diff() returns a DateInterval object, however, it doesn't differentiate between different length years or months:
- "2019-01-01" — "2020-01-01" = 1 year (365 days)
- "2020-01-01" — "2021-01-01" = 1 year (366 days)
- "2019-01-01" — "2019-02-01" = 1 month (31 days)
- "2019-02-01" — "2019-03-01" = 1 month (28 days)
Is there a nice built-in I haven't found yet that can do this, or will I have to roll my own function?
DateTime::diff()might hav already been written. - CJ Dennis