Convert seconds to timestamp?

4

I am creating a PHP script to generate a json from a webvtt caption file, I get the start and end that are in the minuto:segundo.milisegundo format 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 being played 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 comparison I would like my PHP already deliver the start and end time of each caption already in the same format as currentTime of the video, how can I do it?

Here's an example:

$start = "00:05.570";
$fim = "00:09.700";

In the above example it would be easy to distinguish that currentTime would be 5.570 and 9.700 how do I convert PHP to that format?

    
asked by anonymous 05.05.2017 / 00:14

2 answers

4

Solution

A solution using regular expressions would be, just making the seconds 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 at Ideone .

Explanation

The solution was entirely based on the native PHP function

05.05.2017 / 02:21
1

Personally I would prefer to work with the PHP function DateTime native

You would have to set the date as 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);
}

It would be up to you to define better how you would like the return, it depends a lot on your need and how you want to work from here / \ forward.

    
05.05.2017 / 02:11