Time format conversion with PHP

If you have a duration for a recipe in the format 1H10M (1 hour, 10 minutes) or 20M (20 minutes). I want to use the format as described in parentheses. I've tried using strtotime()

with no luck.

Any help would be greatly appreciated.

+3


source to share


4 answers


<?php
$duration="1H10M5S";
$display=str_replace(array('H','M','S'),
         array(' Hour(s) ',' Minute(s) ',' Seconds'),
         $duration);
echo $display;

      

Output



1 Hour(s) 10 Minute(s) 5 Seconds 

      

Fiddle

+3


source


$yourtext = '1H10M';
$newFormat = str_replace("H", " hour,", $yourtext);
$newFormat = str_replace("M", " minutes,", $newFormat);
echo $newFormat;

      



0


source


$date_work = new DateTime($variable_name); $inte = $date_work->format('h:i:s');echo $inte;

      

0


source


<?php

function getBetween($content, $start, $end) {
    $r = explode($start, $content);
    if (isset($r[1])) {
        $r = explode($end, $r[1]);
        return $r[0];
        }
    return '';
}

$dur = "1H2M3S";

$hr = strtok($dur, "H");
$min = getBetween($dur, "H", "M");
$sec = getBetween($dur, "M", "S");

      

Do what you want to do with $ hr, $ min and $ sec.

0


source







All Articles