Search code examples
phpstringtimedate-formattingdate-parsing

Time conversion using php


Is there any way to convert "01:10:00" string into the following format "1 h:10 min" using php? I did this way, but I need to do the opposite.

'date('H:i:s',strtotime('1 hour 1 minute 1 second', strtotime('midnight')))'

Solution

  • There're endless ways, e.g.:

    $input = '01:10:00';
    list($h, $m, $s) = explode(':', $input);
    $output = "$h h:$m min";
    

    You can also tweak output format in different ways, e.g.

    $output = sprintf("%d h:%02d min", $h, $m);
    

    ... will display $h as integer and $m as two-character integer with leading zeroes. It's all just text at this point (don't get confused by the concept of "time").