Last active
September 16, 2023 09:14
-
-
Save RundesBalli/10e62a74c08dc2818f7a7f061e9306dd to your computer and use it in GitHub Desktop.
formatTime function format a given time in seconds to a readable time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Function to format a given time in seconds to a readable time. | |
* | |
* @param int $seconds The seconds to format. | |
* | |
* @return string The formatted time string. | |
*/ | |
function formatTime(int $seconds) { | |
$lang = [ | |
'time' => [ | |
'd' => 'day', | |
'dP' => 'days', | |
'h' => 'hour', | |
'hP' => 'hours', | |
'm' => 'minute', | |
'mP' => 'minutes', | |
's' => 'second', | |
'sP' => 'seconds', | |
], | |
]; | |
$string = ''; | |
$d = floor($seconds / 86400); | |
if($d > 0) { | |
$seconds-= ($d * 86400); | |
$string.= $d.' '.$lang['time']['d'.($d != 1 ? 'P' : NULL)]; | |
} | |
$h = floor($seconds / 3600); | |
if($h > 0) { | |
$seconds-= ($h * 3600); | |
$string.= (!empty($string) ? ', ' : NULL).$h.' '.$lang['time']['h'.($h != 1 ? 'P' : NULL)]; | |
} | |
$m = floor($seconds / 60); | |
if($m > 0) { | |
$seconds-= ($m * 60); | |
$string.= (!empty($string) ? ', ' : NULL).$m.' '.$lang['time']['m'.($m != 1 ? 'P' : NULL)]; | |
} | |
if($seconds > 0) { | |
$string.= (!empty($string) ? ', ' : NULL).$seconds.' '.$lang['time']['s'.($seconds != 1 ? 'P' : NULL)]; | |
} | |
return $string; | |
} | |
/** | |
* Example function calls: | |
* | |
* php > echo formatTime(4561); | |
* 1 hour, 16 minutes, 1 second | |
* | |
* php > echo formatTime(45641); | |
* 12 hours, 40 minutes, 41 seconds | |
* | |
* php > echo formatTime(945641); | |
* 10 days, 22 hours, 40 minutes, 41 seconds | |
*/ | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MIT License | |
Copyright (c) 2023 RundesBalli | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment