简体   繁体   中英

How to convert 00:03:30 in 3m30s

I search for my problem on php.net and on stackoverflow and I did'nt find a good answer to solve my problem, so I decide to ask it !

I have a number of secs :

210

I transform it to 00:03:30 :

gmdate('H:i:s', 210);

But how can I format my answer like : 3m30s or 3min30secs ?

I think I can make this transformation by Exploding my results and concatenate with variable, but I don't know if it's the best solution...

$part = explode(":", $mytime);
$hours = $part[0];
$mins = $part[1];
$secs = $part[2];

$hoursvar = "h";
$minsvar = "m";
$secsvar = "s";

$timefinal = $hours.$hoursvar.$mins.$minsvar.$secs.$secsvar;

Can anyone help me? Thanks.

echo ltrim(date("H", 210), "0")." hours, ".
     ltrim(date('i', 210), "0")." min, ".
     ltrim(date('s', 210), "0")." secs";

date() works a little bit like printf() . You pass a format string containing a defined set of meta characters which will be replaced by values. Note that the format string can contain any content not just meta characters. The manual page explains this as well. You can use:

date('i\ms', 210);

You see that you have to escape literals (the m ) to that they were not replaced by values.

I hope you can live with a leading zero : 03m30

if not, use:

$date = date('i\ms', 210);
if(strpos($date, '0') === 0) {
    $date = substr($date, -strlen($date) + 1);
}

Or, shorter, thanks @Orangepill :) :

$date = ltrim(date('i\ms', 210), '0');

as there is no meta character for minutes without leading zeros known by date() .

You can format the string inside gmdate, just escape the characters you want to print:

$sec = 210;
echo gmdate("i\m s\s", $sec);

Output:

03m 30s

This is easy, your current date format is H:i:s and you want to convert it to i:s

So you do the following

$date = "00:03:30";
$new_date = date("i_s-",strtotime($date));

Since we can't insert the letters m and s in the function we can use str_replace to replace the _ with m and replace - with s so

$new_date = str_replace("_","m",$new_date);
$new_date = str_replace("-","s",$new_date);

That was the best way I could think of.

$string_time = '00:03:30';
$int_time = strtotime(date('Y-m-d').' '.$string_time);
echo date('i', $int_time).'m'.date('s', $int_time).'s';
$seconds = 210;
$date = gmdate("H\h:i\m:s\s", $seconds);
echo preg_replace('/0([0-9])/','$1', $date);

OUTPUT

0h:3m:30s

You can use gmdate with preg_replace to remove 0 from begining.

OR more advanced

$seconds = 210;
$date = gmdate("H\h:i\m:s\s", $seconds);
$match = array('/0([0-9])/','/s/','/m\:/','/h\:/');
$replace = array('$1', ' sec ', ' min ', ' hours ');
echo preg_replace($match,$replace, $date);

OUTPUT:

0 hours 3 min 30 sec
echo ltrim(date("H", 210), "0")." hours, ".
     ltrim(date('i', 210), "0")." min, ".
     ltrim(date('s', 210), "0")." secs";

Thanks to Orangepill !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM