Question

How to convert time with php?

the original format was like 00:02:34 min.

I need to get a echo like 154. Thanks.

Was it helpful?

Solution

Try this:

<?php
   date_default_timezone_set('GMT');
   $time = '00:00:34';
   list ($days, $hours, $minutes) = split(":", $time, 3);
   $timestamp = mktime ($hours+1, $minutes, 0, $days+1, 1, 1970, FALSE);
   $minutes = $timestamp / 60;
   print $minutes;
   print "\n";`

(edit: date_default_timezone_set added)

OTHER TIPS

$myTime="00:02:34 min";
$bodytag = str_replace(" min", "", $myTime);
$myTime=explode(":",$myTime);

function timeToSeconds($hours,$minutes,$seconds){
   return $seconds+($minutes*60)+($hours*3600);
}

Hey yuli chika, I'll suggest you check out php's date and time functions in the manual. From there you can also learn about string to time functions like, well, strtotime() and mktime;

Date and time allow you to specify a format and a timestamp for output. The reference for the format codes are listed in the manual links I gave above. If you don't have a timestamp, you can use strtotime() to create one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top