From 9518ef07d264c70bdb231dbaa69ca4a3ab5eeceb Mon Sep 17 00:00:00 2001 From: Niclas Norin <103985736+NiclasNorin@users.noreply.github.com> Date: Wed, 18 Dec 2024 10:50:29 +0100 Subject: [PATCH] fix: correct event duration (#247) * fix: correct event duration * test: add unit test for getFormattedTimeDiff() in SingleEventData --------- Co-authored-by: Thor Brink --- source/php/Helper/SingleEventData.php | 6 ++--- .../phpunit/tests/Helper/SingleEventData.php | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 tests/phpunit/tests/Helper/SingleEventData.php diff --git a/source/php/Helper/SingleEventData.php b/source/php/Helper/SingleEventData.php index 4dbf1705..43534a58 100644 --- a/source/php/Helper/SingleEventData.php +++ b/source/php/Helper/SingleEventData.php @@ -106,7 +106,7 @@ public static function singleEventDate($postId = null, $dateTime = null) */ public static function getFormattedTimeDiff($start, $end) { - $diff = ($end - $start) / 60 / 60; + $diff = ($end - $start) / 3600; $singleEventData = new SingleEventData(); @@ -122,8 +122,8 @@ public static function getFormattedTimeDiff($start, $end) public function eventOccasionDuration($from, $to) { $diff = (int)abs($to - $from); if ($diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS) { - $hours = round($diff / HOUR_IN_SECONDS ); - $mins = round($diff / MINUTE_IN_SECONDS) - ($hours * 60); + $hours = floor($diff / HOUR_IN_SECONDS ); + $mins = floor($diff / MINUTE_IN_SECONDS) - ($hours * 60); $formattedDiff = sprintf(_n('%s hour', '%s hours', $hours), $hours); if ($mins > 0) { $formattedMins = sprintf(_n('%s min', '%s mins', $mins), $mins); diff --git a/tests/phpunit/tests/Helper/SingleEventData.php b/tests/phpunit/tests/Helper/SingleEventData.php new file mode 100644 index 00000000..e20e786d --- /dev/null +++ b/tests/phpunit/tests/Helper/SingleEventData.php @@ -0,0 +1,22 @@ +assertEquals('1 hour 31 mins', SingleEventData::getFormattedTimeDiff($start, $end)); + } +} \ No newline at end of file