-2

Could anyone solve my problem please. I have mysql table with time_start 'hour:minute' and time_end 'hour:minute' how can i get cumulative time duration between time_start and time_end. thanks,

$sel = $db->query("SELECT * FROM logbook WHERE date = '$date' ");

while($sel_row = mysqli_fetch_array($sel))
{
  $duration = (strtotime($sel_row['time_end'])-strtotime($sel_row['time_start']))/(60*60);        
  $hour = floor($duration);
  $minute = ($duration - $hour)*60;
  echo $hour.":".$minute;
  ? HERE HOW CAN I CUMULATE THE $hour:$minute       
}
Rdb
  • 59
  • 2
  • 12
  • 2
    Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun [deprecation process](http://goo.gl/q0gwD). See the [red box](http://goo.gl/OWwr2)? Instead you should learn about [prepared statements](http://goo.gl/orrj0) and use either [PDO](http://goo.gl/TD3xh) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://goo.gl/YXyWL) will help you. If you pick PDO, [here is good tutorial](http://goo.gl/b2ATO). Also see [Why shouldn't I use mysql functions in PHP?](http://goo.gl/J5jAo) – Daedalus Nov 05 '12 at 05:43
  • @Daedalus: As informative as your answer is, please do not obfuscate the links through a redirect service. Thanks. – dotancohen Nov 05 '12 at 07:20
  • @dotancohen It's a comment, not an answer. The links are shortened because otherwise, the comment would not fit for the allotted characters. This comment would be impossible to post otherwise, and it's a necessary evil considering the circumstances. The reputation of a user _usually_ expresses the trust the community has in that user. I would think that if my comment was damaging, it would be shortly removed. I'm going to continue 'obfuscating' links to inform people of PDO and MySQLi, thanks. – Daedalus Nov 05 '12 at 07:27
  • @Daedalus: Your reputation doesn't show in comments! In any case, I had moused over your links to see if they were documents that I am already familiar with, if I could learn something from them. I cannot do that obfuscated. Also, I haven't had problems with the Google service you use, but I have had issues where bit.ly was down. However, I understand the predicament regarding length. Please mention that issue [here](http://meta.stackexchange.com/questions/126238/do-not-consider-characters-in-links-or-markup-for-comment-text). Thanks! – dotancohen Nov 05 '12 at 08:18
  • @dotancohen Actually, it does. Simply mouse-over the username. Also, I wasn't the original author of the comment. .. I honestly don't know who is, but I've seen this specific comment hundreds of times before on this website. Asking around, I found this: https://gist.github.com/3941118, and after checking it out, added it to a script I use to assist with canned responses. – Daedalus Nov 05 '12 at 08:18

1 Answers1

0

Do the summation where you calculate the duration

$duration = 0;
while($sel_row = mysqli_fetch_array($sel))
{
  $duration += (strtotime($sel_row['time_end'])-strtotime($sel_row['time_start']))/(60*60);        
}
$hour = floor($duration);
$minute = ($duration - $hour)*60;
echo $hour.":".$minute;

Or better, let MySQL do the calculation:

SELECT (SUM(time_end) - SUM(time_start)) / 3600 FROM logbook WHERE date = '$date'; 
Terje D.
  • 6,250
  • 1
  • 22
  • 30