Skip to content

Commit

Permalink
fix: time diff calculation for activities
Browse files Browse the repository at this point in the history
An error in the logic existed due to a lack of documentation. As a result, the
estimated times were off by up to 30 days.

As an example, an activity that takes place on February 14, 2025 at 17:00 would
state that this was in "1 month and 5 days" (at the time of writing this commit
message). We all know that this is not the case, because that would actually be
January 21, 2025.

The `d` property of a `DateInterval` is now used directly instead of doing our
own calculations with the `days` property (_all_ days between both dates, not
just the difference after substracting years and months).
  • Loading branch information
tomudding committed Dec 15, 2024
1 parent 225c29d commit b5b90f0
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions module/Application/src/View/Helper/TimeDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,23 @@ public function __invoke(
DateTime $end,
): string {
$diffInterval = $start->diff($end);
$days = false === $diffInterval->days ? 0 : $diffInterval->days;

$diff = [
'year' => $diffInterval->y,
'month' => $diffInterval->m,
'week' => (int) floor($days / 7) % 4,
'day' => $days % 7,
'week' => (int) floor($diffInterval->d / 7),
'day' => $diffInterval->d % 7,
'hour' => $diffInterval->h,
'minute' => $diffInterval->i,
'second' => $diffInterval->s,
];

$units = [];
$result = [];
if (0 === $days) {
if (
0 === $diff['year']
&& 0 === $diff['month']
&& 0 === $diff['day']
) {
if (
0 === $diff['hour']
&& 0 === $diff['minute']
Expand Down

0 comments on commit b5b90f0

Please sign in to comment.