Skip to content

Instantly share code, notes, and snippets.

@NaWer
Last active April 7, 2022 13:58
Show Gist options
  • Save NaWer/8002905 to your computer and use it in GitHub Desktop.
Save NaWer/8002905 to your computer and use it in GitHub Desktop.
Convert duration in second to duration in ISO8601
<?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;
}
?>
@halid96
Copy link

halid96 commented Apr 7, 2022

❤❤❤ ty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment