DEV Community

Peter Perkins
Peter Perkins

Posted on

DateTime->diff | Does It Round? (no)

In working with differences between timestamps I had a question that may seem trivial, however with the answer not clearly defined in the manual, I wanted to write up my findings 🤓

The question: Does the ->diff function that can be used with DateTime() round to the nearest day? For example:

error_log("DateTime round question");

$date_start = new DateTime('2021-10-22 00:00:00');
$date_end = new DateTime('2021-10-22 23:59:59');
$difference = $date_start->diff($date_end);

error_log("timeDifference: " . $difference->days);
Enter fullscreen mode Exit fullscreen mode

The result to this is: timeDifference: 0;

The time differences are 1 second from being 1 day apart and the result of the difference is 0 days - perfect!

Top comments (0)