From 3e14f06a48120666a009adb97f244475c34604b3 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 2 Aug 2019 14:56:49 -0500 Subject: [PATCH] Fix issue with activities longer than 24h --- lib/WPStrava/SOM.php | 6 +++++- readme.txt | 1 + tests/WPStrava/SOMEnglishTest.php | 12 ++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/WPStrava/SOM.php b/lib/WPStrava/SOM.php index 485bfb8..11c4edb 100644 --- a/lib/WPStrava/SOM.php +++ b/lib/WPStrava/SOM.php @@ -33,9 +33,13 @@ abstract class WPStrava_SOM { * Create a time string of hours:minutes:seconds from just seconds. * * @return string Time formatted as 'H:i:s'. + * @see https://stackoverflow.com/a/20870843/2146022 */ public function time( $seconds ) { - return date( 'H:i:s', mktime( 0, 0, $seconds ) ); + $zero = new DateTime( '@0' ); + $offset = new DateTime( "@{$seconds}" ); + $diff = $zero->diff( $offset ); + return sprintf( '%02d:%02d:%02d', $diff->days * 24 + $diff->h, $diff->i, $diff->s ); } /** diff --git a/readme.txt b/readme.txt index 3921aeb..497e39b 100644 --- a/readme.txt +++ b/readme.txt @@ -86,6 +86,7 @@ WP-Strava caches activity for one hour so your site doesn't hit the Strava API o = 1.7.2 = Added setting to hide elevation. +Fixed hours for activities greater than 24 hours. = 1.7.1 = diff --git a/tests/WPStrava/SOMEnglishTest.php b/tests/WPStrava/SOMEnglishTest.php index f72d9cd..65cc3a5 100644 --- a/tests/WPStrava/SOMEnglishTest.php +++ b/tests/WPStrava/SOMEnglishTest.php @@ -80,4 +80,16 @@ class WPStrava_SOMEnglishTest extends TestCase { $this->assertEquals( '01:20:05', $this->som->time( 4805 ) ); } + + /** + * Test that 221071 seconds is 61:24:31 time (H:i:s) using both string and float inputs. + * + * @author Justin Foell + * @since 1.7.2 + */ + public function test_time_greater24h() { + $this->assertEquals( '61:24:31', $this->som->time( '221071' ) ); + $this->assertEquals( '61:24:31', $this->som->time( 221071 ) ); + } + }