Text time to numbers

I have some lines over time

"42 sec"
"1 min"
"2 h 32 min"

Is it possible to convert this to

"00:00:42"
"00:01:00"
"02:32:00"

with php?

+3
source share
4 answers

PHP strtotime()is a useful function that can convert a string representation of time to a unix timestamp. From this, we can convert time to any format we like.

However, the original temporary lines are not in a format that strtotime()can handle directly. eg. "h" should be "hour". But we could replace them before going to strtotime () if your data is consistent.

Note that we are trying to convert the original time to 0, not the current time.

$rawTimes = array ('42 sec', '1 min', '2 h 32min');
foreach ($rawTimes as $rawTime) {
  // Convert into a format that strtotime() can understand
  $rawTime = str_replace('h','hour',$rawTime);
  echo '"'.$rawTime.'" = "'.gmdate('H:i:s',strtotime($rawTime,0)).'"'."\n";
}

:

"42 sec" = "00:00:42"
"1 min" = "00:01:00"
"2 hour 32min" = "02:32:00"

, strtotime() "", "", "", "" "". , , , . "32 " "32 " .

+7

, :

<?
    $time[] = "42 sec";
    $time[] = "1 min";
    $time[] = "2 h 32min";

    $seconds = '/([\d]{1,})\s?sec/';
    $minutes = '/([\d]{1,})\s?min/';
    $hours = '/([\d]{1,})\s?h/';

    foreach( $time as $t )
    {
        echo '<br />';
        preg_match( $hours, $t, $h );
        $hour = $h[1];
        if( $hour )
        {
            if( strlen( $hour )<2 )
                $hour = '0' . $hour;
        }
        else
            $hour = '00';


        preg_match( $minutes, $t, $m );
        $min = $m[1];
        if( $min )
        {
            if( strlen( $min )<2 )
                $min = '0' . $min;
        }
        else
            $min = '00';

        preg_match( $seconds, $t, $s );
        $sec = $s[1];
        if( $sec )
        {
            if( strlen( $sec )<2 )
                $sec = '0' . $sec;
        }
        else
            $sec = '00';

        echo $hour . ':' . $min . ':' . $sec;

    }
?>

:

00:00:42
00:01:00
02:32:00
+1

I am sure there is a better solution, but it works.

function timetotime($str) {
    $match = null;
    if (preg_match("/(\d+)(\s?)h/", $str, &$match)) {
        $h = $match[1];
        if ($h < 10)
            $result = "0{$h}:";
        else
            $result = "{$h}:";
    } else 
        $result = "00:";

    if (preg_match("/(\d+)(\s?)min/", $str, &$match)) {
        $h = $match[1];
        if ($h < 10)
            $result .= "0{$h}:";
        else
            $result .= "{$h}:";
    } else
        $result .= "00:";

    if (preg_match("/(\d+)(\s?)sec/", $str, &$match)) {
        $h = $match[1];
        if ($h < 10)
            $result .= "0{$h}";
        else
            $result .= "{$h}";
    } else
        $result .= "00";

    return $result;
}
0
source

http://php.net/manual/en/function.strtotime.php

<?php
$dates = array("42 sec", "1 min", "2 hour 32 min"); // normalise last item

foreach($dates as $d)
    printf("%5s - %s\n", $d, nSecs($d));

function nSecs($date)
{
    $t = strtotime("+1 day $date");
    return $t - strtotime("+1 day");
}
?>

If you can normalize h => hour, this is not so bad.

-1
source

Source: https://habr.com/ru/post/1757562/


All Articles