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: correct event duration #247

Merged
merged 2 commits into from
Dec 18, 2024
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
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));
}
}
Loading