Jump to content

Time calculation problem


ramli

Recommended Posts

I want to calculate a total time using the following script:

[code]

while($r1 = mysql_fetch_array($Query1))
{

$difference = $r1['difference']; // time in format 15:15:30
$difference = microtime($difference);  // Transorm to milliseconds
$verschil = $verschil + $difference; //Add differance of every record to the total difined as $verschil

}

$seconds = floor(($verschil)); //calculate seconds of the total
$minutes = floor(($verschil / 60)); //calculate hours of the total
$hours = floor(($verschil / 60) /60);//calculate seconds of the total



[/code]

where difference is a database object in the format 00:00:00.

I dont seem to get it to work wat am i doing wrong ?

Thanx in advance....
Link to comment
Share on other sites

try[code]<?php
$a = array('07:43:09','11:51:51');
$total = 0;
foreach ($a as $b){
$x = explode(':',$b);
$tmp = 0;
foreach ($x as $c) {$tmp = $tmp * 60 + $c;}
$total += $tmp;
}
$out = array();
for ($i=0; $i<3; $i++){
$t = floor($total / 60);
$out[] = sprintf( "%02d", $total - $t * 60);
$total = $t;
}
echo $out = implode(':',array_reverse($out));
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.