<?php
function scholar_parse_time($time)
{
if (preg_match('/^\s*(?P<hour>\d+)(?>:(?P<minute>\d+))(?>:(?P<second>\d+(\.\d*)?))?\s*$/', $time, $match)) {
$h = intval($match['hour']);
$m = isset($match['minute']) ? intval($match['minute']) : null;
$s = isset($match['second']) ? floatval($match['second']) : null;
if ($h < 24 && (null === $m || $m < 60) && (null === $s || $s < 60)) {
if (null === $s) {
$is = null;
$ms = null;
} else {
$is = intval($s);
$ms = round(($s - $is) * 1000);
}
return array(
'hour' => $h,
'minute' => $m,
'second' => $is,
'millis' => $ms,
'iso' => null === $m
? sprintf('%02d', $h)
: (null === $s
? sprintf('%02d:%02d', $h, $m)
: sprintf('%02d:%02d:%02d', $h, $m, $s)
),
);
}
}
return false;
}
var_dump(scholar_parse_time('0:1'));
var_dump(scholar_parse_time('0:1:1'));
var_dump(scholar_parse_time('0:1:1.1'));
var_dump(scholar_parse_time('0:1:1.'));
preferences:
36.49 ms | 402 KiB | 5 Q