Convert seconds to timestamp?
I am creating a PHP script to generate a json
from a subtitle file webvtt
, I take the start and end that are in the format minuto:segundo.milisegundo
or if the video is too large they come as hora:minuto:segundo.milisegundo
for future comparisons in JS in the future where the video is playing I need to compare these values with the currentTime
of the video which in turn delivers the time in seconds segundos.milisegundos
and to facilitate such a comparison I would like my PHP to already deliver the start time and end of each caption already in the same format as the currentTime
of the video, How can I do?
Here's an example:
$start = "00:05.570";
$fim = "00:09.700";
In the example above it would be easy to distinguish that currentTime
would be 5.570
and 9.700
How do I make PHP convert to this format?
2 answers
Solution
A solution using regular expressions would be, making only the required seconds value:
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 the native PHP functionpreg_match
:
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
The first parameter, $pattern
, is the regular expression that we will analyze. The second, $subject
, is the string on which we will apply the expression and the third parameter, $matches
, will be a array with the values of the string that have married 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 the hours, ((?P<hours>\d+)\:)?
, can be reduced to (\d+\:)?
, which means one or more digits (\d+
) followed by a character : (\:
) optional (?
). The ?P<hours>
part serves only to name the group; that is, if there is a value that matches this pattern, create in $matches
the index hour
with the married value. For example, if the input is 01:20:03.7345
, then $matches["hours"]
equals 01
. If the time is not set, $matches["hours"]
will be false
(since we defined that it was optional in the regular expression).
Regular expression: minutes
See hours (exactly the same logic, just changing the group name to minutes
, ((?P<minutes>\d{1,2})\:)?
). We also changed the quantifier from +
to {1,2}
, because the minutes will have 1 or 2 digits: 1 if it is less than 10 minutes (considering that it may not be added 0 to the left) or 2 digits when from 10 to 59 minutes.
Regular expression: seconds
Is basically the same expression used for the minutes, differentiating only that it will be mandatory, so there will be no character ?
at the end, (?P<seconds>\d{1,2})
.
Expression regular: Millis
For milliseconds, (\.(?P<milis>\d+))?
, gets: if set (?
), it should start with the character . (\.
) followed by one or more digits (\d+
), capturing this group as milis
. Notice that \.
is not part of the named group, as we only want the numeric value, otherwise $matches["milis"]
would be something like .570
instead of 570
.
Personally I would prefer to work with the function DateTime
native to PHP
You would have to set the date to 01/01/1970, the rest it would do the conversion...
echo timestamp('00:00:30.570')."<br/>"; //30.57
echo timestamp('00:10:30.570')."<br/>"; // 630.57
echo timestamp('12:10:30.570')."<br/>"; // 43830.57
function timestamp($horario){
list($sec, $msec) = explode('.', $horario);
$date = '01/01/1970 '.$sec;
$dateTime = DateTime::createFromFormat('d/m/Y H:i:s', $date, new DateTimeZone('GMT'));
$timestamp = $dateTime->getTimestamp();
return $timestamp+($msec/1000);
}
Ai would be in charge of you to better define how you would like the return, it depends a lot on your need and how you plan to work from here /\ going forward