A library and webservice I am using communicates time-intervals in ISO 8601 format: PnYnMnDTnHnMnS. I want to convert such formats to seconds. And vice versa. Seconds are a
function parsePTTime($pt_time)
{
$string = str_replace('PT', '', $pt_time);
$string = str_replace('H', 'Hour', $string);
$string = str_replace('M', 'Minute', $string);
$string = str_replace('S', 'Second', $string);
$startDateTime = '19700101UTC';
$seconds = strtotime($startDateTime . '+' . $string) - strtotime($startDateTime);
return $seconds;
}
Tests:
PT1H - OK
PT23M - OK
PT45S - OK
PT1H23M - OK
PT1H45S - OK
PT23M45S - OK
PT1H23M45S - OK