Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(activity): wrong calculation of time difference #1937

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions module/Activity/view/activity/activity/list.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ use Laminas\View\Renderer\PhpRenderer;
$isOngoing = $currentTime >= $beginTime && $currentTime <= $endTime;
?>
<p>
<?= $dateTimeString ?> <small class="text-muted">
<?= ucfirst($dateTimeString) ?> <small class="text-muted">
<?php if ($isOngoing): ?>
<?= $this->translate('for another') ?> <?= $timeRemaining = $this->timeDiff($currentTime, $endTime) ?>
<?= $this->translate('for') ?> <?= $timeRemaining = $this->timeDiff($currentTime, $endTime) ?>
<?php elseif ($currentTime < $beginTime): ?>
<?= $this->translate('in') ?> <?= $this->timeDiff($currentTime, $beginTime) ?>
<?php endif; ?>
Expand Down
14 changes: 10 additions & 4 deletions module/Application/language/en.po

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions module/Application/language/gewisweb.pot

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 13 additions & 8 deletions module/Application/language/nl.po

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 57 additions & 15 deletions module/Application/src/View/Helper/TimeDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
use Laminas\Mvc\I18n\Translator;
use Laminas\View\Helper\AbstractHelper;

use function ceil;
use function array_pop;
use function array_slice;
use function count;
use function floor;
use function implode;
use function sprintf;

class TimeDiff extends AbstractHelper
{
Expand All @@ -20,26 +25,62 @@ public function __invoke(
DateTime $start,
DateTime $end,
): string {
$units = [
'year' => 31536000,
'month' => 2592000,
'week' => 604800,
'day' => 86400,
'hour' => 3600,
'minute' => 60,
];
$diffInterval = $start->diff($end);
$days = false === $diffInterval->days ? 0 : $diffInterval->days;

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

foreach ($units as $unit => $value) {
if ($diff >= $value) {
$number = ceil($diff / $value);
$units = [];
$result = [];
if (0 === $days) {
if (
0 === $diff['hour']
&& 0 === $diff['minute']
) {
$result[] = $this->translator->translate('less than a minute');
} elseif (0 === $diff['hour']) {
$units = ['minute', 'second'];
} else {
$units = ['hour', 'minute'];
}
} else {
$units = ['year', 'month', 'week', 'day'];
}

return $number . ' ' . $this->translator->translatePlural($unit, $unit . 's', (int) $number);
foreach ($units as $unit) {
if (0 === $diff[$unit]) {
continue;
}

$result[] = sprintf(
'%d %s',
$diff[$unit],
$this->translator->translatePlural($unit, $unit . 's', $diff[$unit]),
);
}

if (1 < count($result)) {
return sprintf(
'%s %s %s',
implode(', ', array_slice($result, 0, -1)),
$this->translator->translate('and'),
array_pop($result),
);
}

if (1 === count($result)) {
return $result[0];
}

return $diff . '' . $this->translator->translatePlural('second', 'seconds', $diff);
return $this->translator->translate('unable to estimate time');
}

/**
Expand All @@ -54,5 +95,6 @@ private function registerTranslations(): void
$this->translator->translatePlural('day', 'days', 1);
$this->translator->translatePlural('hour', 'hours', 1);
$this->translator->translatePlural('minute', 'minutes', 1);
$this->translator->translatePlural('second', 'seconds', 1);
}
}
Loading