<?php
echo time_to_iso8601_duration(strtotime("2 years 1 hour 30 minutes", 0));
function time_to_iso8601_duration($time) {
$units = array(
"Y" => 365*24*3600,
"D" => 24*3600,
"H" => 3600,
"M" => 60,
"S" => 1,
);
$str = "P";
$istime = false;
foreach ($units as $unitName => &$unit) {
$quot = intval($time / $unit);
$time -= $quot * $unit;
$unit = $quot;
if ($unit > 0) {
if (!$istime && in_array($unitName, array("H", "M", "S"))) { // There may be a better way to do this
$str .= "T";
$istime = true;
}
$str .= strval($unit) . $unitName;
}
}
return $str;
}
?>