943,193 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2955
  • PHP RSS
Jan 17th, 2010
0

How to calculate total time duration from row results

Expand Post »
Hi,

I have a query that display's results from my sql database. I have it listing a start time and an end time. And I have a script that calculates the duration of how long the service was an outage or was just degraded. I am wanting to display a time total, so gathering the row results and the script that calculates the duration. How can I make it calculate ALL of the displayed services total time based upon each individual service name? Every time I try, it's only calculating each row's hours and minutes and not counting all of the $hours and $minutes and calculating them to provide a total.

Here's my query thus far:

php Syntax (Toggle Plain Text)
  1. // ##### View DB data ##### Start ->
  2. $str = str_replace("\n", "<BR />", $str);
  3. if(!isset($cmd))
  4. {
  5. $result = mysql_query("SELECT *, DATE_FORMAT(ss_datestart, '%d-%m-%Y') AS ss_datestartf, DATE_FORMAT(ss_dateend, '%d-%m-%Y') AS ss_dateendf
  6. FROM tbl_services
  7. WHERE ss_datestart
  8. BETWEEN '$datemonthdate'
  9. AND '$datetodaydate'
  10. ORDER BY ss_closed ASC, ss_datestart DESC, ss_timestart DESC");
  11.  
  12. echo "<table width='100%' cellpadding='0' cellspacing='0' border='1'>";
  13. echo "<tr>";
  14. echo "<td valign='top'>Service Name</td>";
  15. echo "<td valign='top'>RT Raised?</td>";
  16. echo "<td valign='top'>Service Resolved?</td>";
  17. echo "<td valign='top'>Date</td>";
  18. echo "<td valign='top'>Time</td>";
  19. echo "</tr>";
  20.  
  21. while($r=mysql_fetch_array($result))
  22. {
  23. $ss_id=$r["ss_id"];
  24. $ss_status=$r["ss_status"];
  25. $ss_closed=$r["ss_closed"];
  26. $ss_service=str_replace("\r\n","<br>",$r[ss_service]);
  27. $ss_comment=str_replace("\r\n","<br>",$r[ss_comment]);
  28. $ss_rt=$r["ss_rt"];
  29. $ss_rt_raised=$r["ss_rt_raised"];
  30. $ss_useropen=$r["ss_useropen"];
  31. $ss_userclose=$r["ss_userclose"];
  32. $ss_datestart=$r["ss_datestartf"];
  33. $ss_dateend=$r["ss_dateendf"];
  34. $ss_timestart=$r["ss_timestart"];
  35. $ss_timeend=$r["ss_timeend"];
  36.  
  37. echo "<tr>";
  38. echo "<td valign='top'>$ss_service</td>";
  39. echo "<td valign='top'>$ss_rt_raised</td>";
  40. echo "<td valign='top'>$ss_closed</td>";
  41. echo "<td valign='top'>";
  42. echo "Date Start: $ss_datestart<br>Date End: $ss_dateend";
  43. echo "</td>";
  44. echo "<td valign='top'>";
  45. echo "Time Start: $ss_timestart<br>Time End: $ss_timeend<br>";
  46. $date1 = "$ss_datestart"; $time1 = "$ss_timestart";
  47. $date2 = "$ss_dateend"; $time2 = "$ss_timeend";
  48. $before = strtotime($date1 . " " . $time1);
  49. $after = strtotime($date2 . " " . $time2);
  50. $diff = $after - $before;
  51. $hours = floor($diff / 3600);
  52. $minutes = floor(($diff - $hours * 3600) / 60);
  53. $seconds = $diff - $hours * 3600 - $minutes * 60;
  54. echo "Duration: $hours hours and $minutes minutes";
  55. echo "</td>";
  56. echo "</tr>";
  57. }
  58. echo "</table>";
  59. echo "<br>";
  60. }
  61. // ##### View DB data ##### End <-
Similar Threads
Reputation Points: 10
Solved Threads: 2
Light Poster
julzk is offline Offline
28 posts
since Nov 2009
Jan 17th, 2010
0
Re: How to calculate total time duration from row results
That's because the time values are not accumulated, but instead just re-initiated.

Try addition operators. Example:
PHP Syntax (Toggle Plain Text)
  1. $Nss_timestart += $ss_timestart; // $Nss_timestart = $Nss_timestart + $ss_timestart;
  2. $time1 = "$Nss_timestart";

Hope it helps, if it doesn't please post back for the profit of any one else who wants to know.
Last edited by CJesusSaves; Jan 17th, 2010 at 1:08 am.
Reputation Points: 16
Solved Threads: 5
Light Poster
CJesusSaves is offline Offline
49 posts
since Jan 2010
Jan 17th, 2010
0
Re: How to calculate total time duration from row results
Hi, I tried this but it did not work (

Just so we understand, my sql tables do not have a hours/minutes/seconds field. They are some simple variables that calculate the time duration from the ss_timestart and ss_timeend.

When I call $hours and $minutes it simply displays the hours and minutes between ss_timestart and ss_timeend from each row.

But I'm wanting to get the data from $hours and $minutes from the displayed looped rows and then sum a total.
Reputation Points: 10
Solved Threads: 2
Light Poster
julzk is offline Offline
28 posts
since Nov 2009
Jan 17th, 2010
0
Re: How to calculate total time duration from row results
When you say it didn't work, what output did you get? If you didn't already, you should accumulate for all:

Edited: It just hit me, you are working with date and time, unless those date/time stamps are not in "integer form" then like you already noticed, the method I posted won't work.

In any order, from your code above, $ss_datestart, $ss_timestart, $ss_dateend , $ss_timeend must be added to their respective previous values. After that, then you can continue to codes on line 48 and after.

It might help if I can see those date/time start/end values. That is, at least show one row result for those values. Then we can figure out how to go about 'adding' them.

Sorry about the previous miss understanding.
Last edited by CJesusSaves; Jan 17th, 2010 at 4:22 am.
Reputation Points: 16
Solved Threads: 5
Light Poster
CJesusSaves is offline Offline
49 posts
since Jan 2010
Jan 17th, 2010
0
Re: How to calculate total time duration from row results
1. Use SUM mysql statement for hours and minutes fields.
2. Change minutes to hours (/60) and keep the remainder as minutes (%60).
3. Collate time elements and labels for output.

Simple.
However, placing time as integer (seconds) in your DB would be easier - you should do this.
Sponsor
Featured Poster
Reputation Points: 1041
Solved Threads: 935
Sarcastic Poster
ardav is offline Offline
6,626 posts
since Oct 2006
Jan 17th, 2010
0
Re: How to calculate total time duration from row results
Here is what my table looks like:

Service X Date Start: 12-01-2010 Date End: 12-01-2010 Time Start: 11:18:46 Time End: 19:06:00
Duration: 7 hours and 47 minutes
Service Y Date Start: 11-01-2010 Date End: 11-01-2010 Time Start: 10:15:00 Time End: 10:21:00
Duration: 0 hours and 6 minutes
Service Z Date Start: 20-12-2009 Date End: 20-12-2009 Time Start: 19:57:51 Time End: 20:53:25
Duration: 0 hours and 55 minutes

The duration of each simply calculates the hours/minutes/seconds from each row as per my original script posting above. but I am wanting to re-use my $hours and $minutes variables from inside the while loop for each row and calculate a total hours and minutes from the displayed results.
Reputation Points: 10
Solved Threads: 2
Light Poster
julzk is offline Offline
28 posts
since Nov 2009
Jan 17th, 2010
0
Re: How to calculate total time duration from row results
Oh hell, julzk, you didn't even format the date to unix. It's in European(?) format:
dd-mm-yyyy.

For a pure mysql solution:

SELECT FLOOR(SUM(UNIX_TIMESTAMP(CONCAT(STR_TO_DATE(end_date,'%d-%m-%Y'), ' ',end_time)) - UNIX_TIMESTAMP(CONCAT(STR_TO_DATE(start_date,'%d-%m-%Y'), ' ',start_time)))/3600) AS hrs, FORMAT(SUM(UNIX_TIMESTAMP(CONCAT(STR_TO_DATE(end_date,'%d-%m-%Y'), ' ',end_time)) - UNIX_TIMESTAMP(CONCAT(STR_TO_DATE(start_date,'%d-%m-%Y'), ' ',start_time)))/60%60,0) AS mins FROM datecalc

Change bold fields/table to your own.

This will give 2 'total' columns:
hrs
mins

If you had proper 'integer' unix timestamps, all this nastiness could be avoided - even if you were using php for the calculation part.
Sponsor
Featured Poster
Reputation Points: 1041
Solved Threads: 935
Sarcastic Poster
ardav is offline Offline
6,626 posts
since Oct 2006
Jan 19th, 2010
0
Re: How to calculate total time duration from row results
Alrighty, figured out the answer myself

Just added the below after my $seconds variable and then I just called $allhourstotal and $minutesleft outside the while loop and it works fine

PHP Syntax (Toggle Plain Text)
  1. $allhours=$allhours+floor($diff / 3600); // Total Hours
  2. $allminutes=$allminutes+floor(($diff - $hours * 3600) / 60); // Total Minutes
  3.  
  4. if ($allminutes >= 60) {
  5. $sparehours = floor($allminutes / 60);
  6. $allhourstotal = $allhours+$sparehours;
  7. $minutesleft = $allminutes-($sparehours*60);
  8. }
Reputation Points: 10
Solved Threads: 2
Light Poster
julzk is offline Offline
28 posts
since Nov 2009
Jan 19th, 2010
0
Re: How to calculate total time duration from row results
OK
Last edited by ardav; Jan 19th, 2010 at 9:08 pm.
Sponsor
Featured Poster
Reputation Points: 1041
Solved Threads: 935
Sarcastic Poster
ardav is offline Offline
6,626 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Category class with unlimited depth
Next Thread in PHP Forum Timeline: input data to an array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC