Last active
April 7, 2022 13:58
-
-
Save NaWer/8002905 to your computer and use it in GitHub Desktop.
Convert duration in second to duration in ISO8601
This file contains hidden or 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 | |
/* | |
* Convert duration in second to duration in ISO8601 | |
* http://www.w3schools.com/Schema/schema_dtypes_date.asp#Duration Data Type | |
* http://en.wikipedia.org/wiki/ISO_8601#Durations | |
* | |
* @param integer $duration in second seconde | |
* @return string duration in ISO8601 | |
* | |
* >= PHP 5.3 : use DateInterval <http://www.php.net/manual/en/dateinterval.construct.php> | |
*/ | |
function duration8601($second) | |
{ | |
$h = intval($second/3600); | |
$m = intval(($second -$h*3600)/60); | |
$s = $second -($h*3600 + $m*60); | |
$ret = 'PT'; | |
if ($h) | |
$ret.=$h.'H'; | |
if ($m) | |
$ret.=$m.'M'; | |
if ((!$h && !$m) || $s) | |
$ret.=$s.'S'; | |
return $ret; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
❤❤❤ ty