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);

Recommended Answers

All 4 Replies

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.

Member Avatar for diafol

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');

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 
Member Avatar for diafol

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.