DEV Community

Peter Perkins
Peter Perkins

Posted on

3 2

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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay