3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function scholar_parse_time($time) { if (preg_match('/^\s*(\d+):(\d+)(:(\d+(?>\.\d*)?))?\s*$/', $time, $match)) { list(, $h, $m,) = $match; // sekundy sa opcjonalne, trzeba je pobrac inaczej $s = isset($match[4]) ? floatval($match[4]) : 0; if ($h < 24 && $m < 60 && $s < 60) { $is = intval($s); $ms = $s - $is; return array( 'hour' => $h, 'minute' => $m, 'second' => $is, 'millis' => round($ms * 1000), 'iso' => sprintf('%02d:%02d:%02d', $h, $m, $is), ); } } 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.0 - 7.1.0
array(5) { ["hour"]=> string(1) "0" ["minute"]=> string(1) "1" ["second"]=> int(0) ["millis"]=> float(0) ["iso"]=> string(8) "00:01:00" } array(5) { ["hour"]=> string(1) "0" ["minute"]=> string(1) "1" ["second"]=> int(1) ["millis"]=> float(0) ["iso"]=> string(8) "00:01:01" } array(5) { ["hour"]=> string(1) "0" ["minute"]=> string(1) "1" ["second"]=> int(1) ["millis"]=> float(100) ["iso"]=> string(8) "00:01:01" } array(5) { ["hour"]=> string(1) "0" ["minute"]=> string(1) "1" ["second"]=> int(1) ["millis"]=> float(0) ["iso"]=> string(8) "00:01:01" }