<?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.'));
- Output for 4.3.3 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- array(5) {
["hour"]=>
int(0)
["minute"]=>
int(1)
["second"]=>
NULL
["millis"]=>
NULL
["iso"]=>
string(5) "00:01"
}
array(5) {
["hour"]=>
int(0)
["minute"]=>
int(1)
["second"]=>
int(1)
["millis"]=>
float(0)
["iso"]=>
string(8) "00:01:01"
}
array(5) {
["hour"]=>
int(0)
["minute"]=>
int(1)
["second"]=>
int(1)
["millis"]=>
float(100)
["iso"]=>
string(8) "00:01:01"
}
array(5) {
["hour"]=>
int(0)
["minute"]=>
int(1)
["second"]=>
int(1)
["millis"]=>
float(0)
["iso"]=>
string(8) "00:01:01"
}
- Output for 4.3.0 - 4.3.2
- Notice: Undefined index: hour in /in/ALSE5 on line 5
array(5) {
["hour"]=>
int(0)
["minute"]=>
NULL
["second"]=>
NULL
["millis"]=>
NULL
["iso"]=>
string(2) "00"
}
Notice: Undefined index: hour in /in/ALSE5 on line 5
array(5) {
["hour"]=>
int(0)
["minute"]=>
NULL
["second"]=>
NULL
["millis"]=>
NULL
["iso"]=>
string(2) "00"
}
Notice: Undefined index: hour in /in/ALSE5 on line 5
array(5) {
["hour"]=>
int(0)
["minute"]=>
NULL
["second"]=>
NULL
["millis"]=>
NULL
["iso"]=>
string(2) "00"
}
Notice: Undefined index: hour in /in/ALSE5 on line 5
array(5) {
["hour"]=>
int(0)
["minute"]=>
NULL
["second"]=>
NULL
["millis"]=>
NULL
["iso"]=>
string(2) "00"
}
preferences:
269.5 ms | 403 KiB | 460 Q