Solution
A solution using regular expressions would be, making only the second value mandatory:
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("00:09.700"), PHP_EOL; // 9.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
See working on Ideone.
Explanation
The solution was entirely based on PHP’s native function preg_match
:
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
The first parameter, $pattern
, is the regular expression we will analyze. The second, $subject
, is the string on which we will apply the regular expression and the third parameter, $matches
, will be a array with the values of string that match the pattern defined in the regular expression.
The regular expression used is divided into four parts:
/(((?P<hours>\d+)\:)?(?P<minutes>\d{1,2})\:)?(?P<seconds>\d{1,2})(\.(?P<milis>\d+))?/
+-----------------+-----------------------+-------------------+------------------+
(horas) (minutos) (segundos) (milis)
Regular expression: hours
The regular expression for hours, ((?P<hours>\d+)\:)?
, may be reduced to (\d+\:)?
, which means one or more digits (\d+
) followed by a character : (\:
) optional (?
). The part ?P<hours>
serves only to name the group; i.e., if there is a value that matches this pattern, create in $matches
the index hour
with the value matched. For example, if the input is 01:20:03.7345
, there will be $matches["hours"]
equal to 01
. If time is not set, $matches["hours"]
will be false
(because we defined that it was optional in regular expression).
Regular expression: minutes
Vide hours (exactly the same logic, just changing the group name to minutes
, ((?P<minutes>\d{1,2})\:)?
). The quantifier of +
for {1,2}
, because minutes will have 1 or 2 digits: 1 if it is less than 10 minutes (whereas 0 may not be added to the left) or 2 digits when 10 to 59 minutes.
Regular expression: seconds
It is basically the same expression used for minutes, differentiating only that it will be mandatory, so there will be no character ?
in the end, (?P<seconds>\d{1,2})
.
Regular expression: Milis
For the milliseconds, (\.(?P<milis>\d+))?
, stays: if defined (?
), must start with the character . (\.
) followed by one or more digits (\d+
), capturing this group under the name of milis
. Note that the \.
is not part of the nominated group as we only want the numerical value, otherwise, $matches["milis"]
would be something like .570
instead of 570
.
@Leocaracciolo not understood, has an example in the question of two values and how they should be delivered by PHP
– Leo Letto
I’m not sure that timestamp understands milliseconds for value conversion....
– MarceloBoni
I don’t know if timestamp would be the correct term, I’ve looked in some places but I always find the opposite of what I need
– Leo Letto
I want to convert a time period that is format in hh:ii:ss. u to ss. u only, ie convert time into seconds
– Leo Letto
Correct Leo 00:05,570 is 5 seconds and 570 ms
– Leo Letto
In your example the first two 00 representing the time, is a dynamic value that may or may not be passed to the variable, if the time to be converted is less than 1 hour, it will not reach it in the full format
– Leo Letto
You want to convert all time in seconds?
– novic