<?php
function convert($value)
{
if (preg_match("/(((?P<hours>\d+)\:)?(?P<minutes>\d{1,2})\:)?(?P<seconds>\d{1,2})(\.(?P<milis>\d+))?/", $value, $matches)) {
$hours = intval($matches["hours"]); $minutes = intval($matches["minutes"]); $seconds = intval($matches["seconds"]); $milis = isset($matches["milis"]) ?
intval($matches["milis"]) : 0;
return sprintf("%d.%d", $hours * 3600 + $minutes * 60 + $seconds, $milis); }
return false;
}
// Entrada: horas:minutos:segundos.milis
echo convert("123:12:42.9"), PHP_EOL; // 443562.9
// Entrada: horas:minutos:segundos.milis
echo convert("01:20:03.7345"), PHP_EOL; // 4803.7345
// Entrada: horas:minutos:segundos.milis
echo convert("0:01:56.23"), PHP_EOL; // 116.23
// Entrada: minutos:segundos.milis
echo convert("00:05.570"), PHP_EOL; // 5.570
// Entrada: minutos:segundos.milis
echo convert("01:09.700"), PHP_EOL; // 69.700
// Entrada: minutos:segundos
echo convert("00:05"), PHP_EOL; // 5.0
// Entrada: segundos.milis
echo convert("4.55"), PHP_EOL; // 4.55
// Entrada: segundos
echo convert("12"), PHP_EOL; // 12.0
PD9waHAKCmZ1bmN0aW9uIGNvbnZlcnQoJHZhbHVlKQp7CiAgICBpZiAocHJlZ19tYXRjaCgiLygoKD9QPGhvdXJzPlxkKylcOik/KD9QPG1pbnV0ZXM+XGR7MSwyfSlcOik/KD9QPHNlY29uZHM+XGR7MSwyfSkoXC4oP1A8bWlsaXM+XGQrKSk/LyIsICR2YWx1ZSwgJG1hdGNoZXMpKQogICAgewogICAgICAgICRob3VycyAgID0gaW50dmFsKCRtYXRjaGVzWyJob3VycyJdKTsKICAgICAgICAkbWludXRlcyA9IGludHZhbCgkbWF0Y2hlc1sibWludXRlcyJdKTsKICAgICAgICAkc2Vjb25kcyA9IGludHZhbCgkbWF0Y2hlc1sic2Vjb25kcyJdKTsKICAgICAgICAkbWlsaXMgICA9IGlzc2V0KCRtYXRjaGVzWyJtaWxpcyJdKSA/IGludHZhbCgkbWF0Y2hlc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