Skip to content

Commit

Permalink
fix: correct event duration (#247)
Browse files Browse the repository at this point in the history
* fix: correct event duration

* test: add unit test for getFormattedTimeDiff() in SingleEventData

---------

Co-authored-by: Thor Brink <[email protected]>
  • Loading branch information
NiclasNorin and thorbrink authored Dec 18, 2024
1 parent fd245dc commit 9518ef0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 3 additions & 3 deletions source/php/Helper/SingleEventData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions tests/phpunit/tests/Helper/SingleEventData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace EventManagerIntegration\tests\Helper;

use EventManagerIntegration\Helper\SingleEventData;
use WP_Mock\Tools\TestCase;

class SingleEventDataTest extends TestCase {

/**
* @testdox getFormattedTimeDiff() should return the formatted time difference between two timestamps without rounding minutes.
*/
public function testGetFormattedTimeDiff() {
define('DAY_IN_SECONDS', 86400);
define('HOUR_IN_SECONDS', 3600);
define('MINUTE_IN_SECONDS', 60);

$start = strtotime('2021-01-01 12:00:00');
$end = strtotime('2021-01-01 13:31:30');
$this->assertEquals('1 hour 31 mins', SingleEventData::getFormattedTimeDiff($start, $end));
}
}

0 comments on commit 9518ef0

Please sign in to comment.