Hello everyone,

can you please help me regarding on converting duration to milliseconds. I have a code below working but need to include the hours.

I do not know what is the best practice regarding this but please advise. Thanks in advance.

``````            list(\$get_minutes, \$get_seconds) = explode(':', '1:27:04');
\$minutes = \$get_minutes * 60;
\$seconds = \$get_seconds % 60;
\$set_duration = (\$minutes + \$seconds);
``````
3
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by diafol

Hi,

I thought 1 minute is equal to 60,000 milliseconds, so your equation can be something like this.

``````list(\$get_minutes, \$get_seconds,\$m_seconds) = explode(':', '1:27:04');
## approximate the milliseconds for some hour
\$minutes = ( \$get_minutes * (60* 60000));
## approximate the milliseconds for some minute
\$seconds = \$get_seconds * 60000;
## approximate the milliseconds for some seconds
\$m_seconds = \$m_seconds * 1000;
## approximate the sum of the approximated values
\$set_duration = (\$minutes + \$seconds + \$m_seconds);

## print the sum to see if is plausible.
echo \$set_duration;
``````

Based on the approximation above, there should be a total of 5,224,000 milliseconds in 1:27:04.

LIke veedeoo's...

``````function time2ms(\$time){
\$bits = explode(":",\$time); //hr|min|sec
return \$bits[0]*3600000 + \$bits[1]*60000 + \$bits[2] * 1000;
}
echo time2ms('3:20:07');
``````

Edited by diafol

I apologize, what I am trying to do is duration to convert to timestamp. This works the way I want to but please give some suggestions. This is what I came up:

``````function time2ms(\$time){
\$duration = \$time; //Hours:Minutes,Seconds
list(\$get_hours, \$get_minutes, \$get_seconds) = explode(':', \$duration);
\$hours = \$get_hours * 3600 ;
\$minutes = \$get_minutes * 60;
\$seconds = \$get_seconds % 60;
\$set_duration = (\$hours + \$minutes + \$seconds);
return \$set_duration;
}
``````

This will be the usage:

``````    \$timestamp = mktime(0, 0, 0, 5, 6, 2012); // return 06/05/2012 00:00:00
echo "<br>".date("d/m/Y H:i:s", \$timestamp );
\$available = time2ms('3:20:07');
echo "<br>".date("d/m/Y H:i:s", (\$timestamp + \$available)); //return 06/05/2012 03:20:07
``````

Unix timestamps are in seconds. Try microtime. Why do you need to go to this precision?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.