Andrew R Posted February 3, 2007 Share Posted February 3, 2007 Hi How do I count up hours in php taking into consideration that there are 60 minutes in an hour and 60 seconds in a minute? For example if I want to count 01:25:00 and 01:30:00 taking into consideration that there are 60 minutes in an hour. If I were to count the two normally in php it would add up to, 2 but I want it to add up to 02:55:00. How would I do that? Cheers Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 3, 2007 Share Posted February 3, 2007 $time = strtotime("01:25:00"); $time = $time + 5400;//since you want to add one and a half hour, therefore 5400 seconds. $time = date('h:i A',$time);//sorry, forget to change the format back Ted Quote Link to comment Share on other sites More sharing options...
Andrew R Posted February 3, 2007 Author Share Posted February 3, 2007 Thanks for that. Although in most cases I'll be counting hours from a database so I won't be able to convert the hours to seconds. For example <?php $sql = mysql_query("SELECT SUM(online_time) AS count FROM users"); $count = mysql_result($sql, 0, 'count'); echo "$count"; ?> In my database all the times (online_time) are in the format of hh:mm:ss (00:00:00) Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 3, 2007 Share Posted February 3, 2007 <?php $sql = mysql_query("SELECT SUM(online_time) AS count FROM users"); $count = mysql_result($sql, 0, 'count'); $time = strtotime($count); $time = $time + 5400; $time = date('h:i A',$time); echo $time; ?> I think this should give what you want. Quote Link to comment Share on other sites More sharing options...
Andrew R Posted February 3, 2007 Author Share Posted February 3, 2007 No luck with that script ted_chou12. What I basically want to do is total up all the hours in the database and echo them taking into consideration how many minutes/seconds there is in an hour. In my database there are over 30 totals to add up. Thanks Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 3, 2007 Share Posted February 3, 2007 can you give me an example of your ideal output? because I dont quite understand minutes/seconds there is in an hour.. Ted Quote Link to comment Share on other sites More sharing options...
Andrew R Posted February 3, 2007 Author Share Posted February 3, 2007 can you give me an example of your ideal output? because I dont quite understand minutes/seconds there is in an hour.. Ted The output I want is something like 00:00:00 (H:i:s) What I mean about the minutes/seconds is that if you normally count hours, minutes and seconds up you get the wrong output. For example if I add up 1 hour, 30 minutes against 1 hour, 25 minutes I want the output to be 02:55:00 rather than just 255. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 3, 2007 Share Posted February 3, 2007 oh! , that gave me the right idea! Whats your table name and the time column name please? Ted Quote Link to comment Share on other sites More sharing options...
Andrew R Posted February 3, 2007 Author Share Posted February 3, 2007 oh! , that gave me the right idea! Whats your table name and the time column name please? Ted Column Name = online_time Table Name = users Hard example: What is 2:45 + 1:20 ? Add the Hours: 2+1 = 3 Add the Minutes: 45+20 = 65 The minutes are 60 or more, so subtract 60 from minutes (65-60 = 5 Minutes) and add 1 to Hours (3+1 = 4 Hours) ... so the answer is 4:05 Above - Best example I can find Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 3, 2007 Share Posted February 3, 2007 I guess this should work, please dont hesitate to tell me if it doesnt: <?php $read = mysql_query("SELECT online_time FROM users"); while($r = mysql_fetch_array($read)){ $time = strtotime($r['online_time']); $array[] = $time; } array_sum($array); $time = date('H:i:s',$time); echo $time; ?> Ted Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 3, 2007 Share Posted February 3, 2007 if that one doesnt, try this one: <?php $read = mysql_query("SELECT online_time FROM users"); while($r = mysql_fetch_array($read)){ $time = explode(":", $r['online_time']) $hour[] = $time[0]; $minute[] = $time[1]; $second[] = $time[2]; } array_sum($hour); array_sum($minute); array_sum($second); while ($second > 60) {$minute + 1;$second - 60;} while ($minute > 60) {$hour + 1;$minute - 60;} $time = "$hour:$minute:$second"; $time = date('H:i:s',$time); echo $time; ?> Ted Quote Link to comment Share on other sites More sharing options...
Andrew R Posted February 3, 2007 Author Share Posted February 3, 2007 There seems to be a syntax error on line 6? Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 3, 2007 Share Posted February 3, 2007 yeah, sorry, corrected it Quote Link to comment Share on other sites More sharing options...
chronister Posted February 3, 2007 Share Posted February 3, 2007 what format do you store the time / date in? If you use unix timestamp e.g. 1175662800, Then I have a nice little script that converts the difference between 2 unix timestamps into readable time. Simply pass the seconds into the function and it returns something like 1:30:25 <?php function convert_seconds ($sec, $padHours = false) { // holds formatted string $hms = ""; // there are 3600 seconds in an hour, so if we // divide total seconds by 3600 and throw away // the remainder, we've got the number of hours $hours = intval(intval($sec) / 3600); // add to $hms, with a leading 0 if asked for $hms .= ($padHours) ? str_pad($hours, 2, "0", STR_PAD_LEFT). ':' : $hours. ':'; // dividing the total seconds by 60 will give us // the number of minutes, but we're interested in // minutes past the hour: to get that, we need to // divide by 60 again and keep the remainder $minutes = intval(($sec / 60) % 60); // then add to $hms (with a leading 0 if needed) $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':'; // seconds are simple - just divide the total // seconds by 60 and keep the remainder $seconds = intval($sec % 60); // add to $hms, again with a leading 0 if needed $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT); // done! return $hms; } ?> Hats off to the chap who wrote it. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 3, 2007 Share Posted February 3, 2007 Here's a better way of doing this: <?php $tt = 0; $midnight = strtotime(date('Y-m-d') . ' 00:00:00'); $read = mysql_query("SELECT online_time FROM users"); while($r = mysql_fetch_assoc($read)) $tt += strtotime($r['online_time']) - $midnight; $hours = floor($tt/3600); $x = $tt - floor($tt/3600)*3600; $mins = floor($x/60); $secs = $x - floor($x/60) * 60; echo 'Total online time is ' . $hours . ':' . $mins . ':' . $secs; ?> Ken Quote Link to comment Share on other sites More sharing options...
Andrew R Posted February 3, 2007 Author Share Posted February 3, 2007 I keeping getting an error on both Teds and Kens scripts. Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 3, 2007 Share Posted February 3, 2007 Then there is something wrong with the query. Change this line: <?php $read = mysql_query("SELECT online_time FROM users"); ?> to <?php $q = "SELECT online_time FROM users"; $read = mysql_query($q) or die("Problem with the query:<pre>$q</pre><br>" . mysql_error()); ?> and see what error is printed. Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.