/** * Translates a time string to seconds * @param $s text such as "18:40:22", "18:40:22.11" or "18:40:22,11" * @return duration in seconds */ function in_seconds($s) { if (!is_hms($s)) { throw new \Exception('not a time string: ' . $s); } $x = explode(':', $s); if (count($x) != 3) { throw new \Exception('bad format: ' . $s); } $x[2] = str_replace(',', '.', $x[2]); return $x[0] * 3600 + $x[1] * 60 + $x[2]; }
if (!is_hms('00:00:00')) { echo "FAIL 51\n"; } if (!is_hms('00:00:00.00')) { echo "FAIL 52\n"; } if (is_hms('123.123.123')) { echo "FAIL 53\n"; } if (!is_hms('12:44:11,21')) { echo "FAIL 54\n"; } if (!is_hms('00:26:36,595')) { echo "FAIL 55\n"; } if (!is_hms('00:00:0,500')) { echo "FAIL 56\n"; } if (parse_duration('4h') != 14400) { echo "FAIL 60\n"; } if (parse_duration('-4h') != -14400) { echo "FAIL 61\n"; } if (num_days('2010-03-04', '2010-03-04') != 1) { echo "FAIL 70\n"; } if (num_days('2010-03-04', '2010-03-06') != 3) { echo "FAIL 71\n"; } if (num_days('2010-01-01', '2010-02-24') != 55) {