473,785 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding together time hh:mm:ss to a total

1 New Member
Hi,

I'm building a time report system for work and I want to show the total hours and minutes a project have taken but can't get it to work ...

I have a database (MySQL) in one table I have the date in one colum and the time in one colum. The date i formated like this YYYY-MM-DD and set to date. And the TIME is like this HH:mm:ss.
My code looks like this right now...

[PHP]<?php
$sql="select
p.id, p.date, p.time
from rap_time p
order by 1 desc";
$rows = mysql_query($sq l);
$totaltime = 0;
while($row = mysql_fetch_arr ay($rows)) {

echo"$row[1]";
echo", ";
echo"$row[2]";
echo"<br />";
$totaltime = $totaltime + $row[2];
}
echo"$totaltime ";
?>
[/PHP]
The $totaltime just shows the total hours and it does not count the hours and minutes together.
So if I have an output of times that looks like this:
01:10:00
01:13:00
01:40:00
the totaltime will show:
3
Not 04:03:00
And yes I'm a noob on PHP and SQL...
May 14 '07 #1
5 27266
Lick
3 New Member
You will have to write code that first counts the seconds and see if they overflow the 59 seconds limit. If they overflow, you add 1 to the minute calculation.

Next, you count the minutes and see if they overflow the 59 minutes limit. If they overflow, you add 1 to the hours calculation.
Note: don't forget to add the 1 from the previous calculation, if applicable.

Next, you count the hours and if applicable, add 1 from the previous calculation.
May 14 '07 #2
pbmods
5,821 Recognized Expert Expert
Here's how I would do it:

Assuming the times don't span multiple days, pick a random day. For example, '2007-05-14' (to pick a random one). Then prepend the date to your times and calculate their unix timestamps.

Then, all you have to do is add up the differences between your times and midnight of the same day:

Expand|Select|Wrap|Line Numbers
  1. var $date = '2007-05-14';
  2. var $datetime = strtotime("$date 00:00:00");
  3.  
  4. //    These are examples; you'll probably want to pull your numbers from the DB.
  5. var $times = array(
  6.     '01:15:45',
  7.     '03:11:02',
  8.     '10:15:45'
  9. );
  10.  
  11. var $sum = 0;
  12. foreach($times as $time) {
  13.     $sum += strtotime("$date $time") - $datetime;
  14. }
  15.  
  16. //    $sum holds the total number of seconds of all three times.
  17. print($sum);
  18.  
When the loop executes the first time, we are computing the difference between two datetimes:

Expand|Select|Wrap|Line Numbers
  1. $sum += strtotime('2007-05-14 01:15:45') - strtotime('2007-05-14 00:00:00');
  2.  
You're taking the number of seconds from January 1, 1979 (or something like that) until May 14, 2007 at 1:15 AM and 45 seconds. Then you take the number of seconds from January 1, 1979 until May 14, 2007 at midnight and subtract the two numbers.

Since May 14, 2007 01:15:45 occurred exactly 1 hour, 15 minutes and 45 seconds after May 14, 2007 00:00:00, $sum has just been +='ed to the number of seconds in 1 hour, 15 minutes and 45 seconds (4515).

Continue through the loop with the rest of your values, and you've got yourself a handy little time-summer.

Now the trick comes into play when your times span >1 day. But the fix for that isn't too difficult ;)
May 15 '07 #3
skillednerd
1 New Member
Expand|Select|Wrap|Line Numbers
  1. list($h, $m, $s) = split('[:]', $time_1_here);
  2. list($h2, $m2, $s2) = split('[:]', $time_2_here);
  3.  
  4. $h = ($h * 60) * 60;
  5. $m = $m * 60;
  6. $h2 = ($h2 * 60) * 60;
  7. $m2 = $m2 * 60;
  8.  
  9.  
  10. $x = $h + $m + $s;
  11. $x2 = $h2 + $m2 + $s2;
  12.  
  13. $y = $x + $x2;
  14.  
  15. $y = $y / 60;
  16. $y = $y / 60;
  17.  
  18. echo $y;
[Please use CODE tags when posting source code. Thanks! --pbmods]
Jul 2 '07 #4
kovik
1,044 Recognized Expert Top Contributor
You know, the reason timestamps were ever used is because you can calculate the difference between them with simple subtraction. You can use mktime() to turn the dates into timestamps, and then do this:

Expand|Select|Wrap|Line Numbers
  1. $end - $start = $timeTaken;
  2. $years = date('Y', $timeTaken) - 1970; // because timestamps start at 1970
  3. $months = date('n', $timeTaken);
  4. $days = date('j', $timeTaken);
Jul 2 '07 #5
helalreza
9 New Member
Expand|Select|Wrap|Line Numbers
  1. <body>
  2. <input type="time" step="1" id="startTimeInput"  name="start_time" value=""><br>     <!--   write as like       02:31:55   -->
  3. <input  type="time" step="1" id="endTimeInput"  name="end_time" value=""><br>
  4. <button id="calcBtn">Seconds</button><br>
  5. <div id="result"></div>
  6. <script type="text/javascript" src="js/jquery.js"></script>
  7. <script type="text/javascript">
  8. document.getElementById('calcBtn').onclick = function(){
  9.     var startTime = document.getElementById('startTimeInput').value;
  10.     var endTime = document.getElementById('endTimeInput').value;
  11.   var result = getDeltaSeconds(startTime, endTime);
  12.   document.getElementById('result').innerHTML = result + ' sec';
  13. }
  14. function getDeltaSeconds(startTime, endTime){
  15.     var startTimeSeconds = getSeconds(startTime);
  16.   var endTimeSeconds = getSeconds(endTime);
  17.   return endTimeSeconds - startTimeSeconds;    
  18. }
  19. function getSeconds(timeString){
  20.     var parts = timeString.split(' ');
  21.   var time = parts[0];
  22.   var ampm = parts[1];
  23.   var timeParts = time.split(':');
  24.   var seconds = Number(timeParts[2]) + Number(timeParts[1]*60) + Number(timeParts[0]*60*60);
  25.   return ampm == 'AM' ? seconds : seconds + 12*60*60;
  26. }
  27. </script>
  28. </body>
  29.  
  30.  
Oct 4 '16 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
3107
by: Able | last post by:
Dear friends I need to format seconds as hh:mm:ss. I see a lot of coding transforming seconds to hh:mm:ss. Somebody know a short way? Regards Able
4
39051
by: Rich | last post by:
Now.ToShortTimeString returns 9:13 PM. I would like to get this in miliatry time with seconds included 21:13:45 (or something like that - just military time with seconds). How can this be done? Thanks, Rich
14
4101
by: Michael Barrido | last post by:
I have this for example: Dim iSeconds as int32 = 3600 '3600 seconds is one hour How do i convert it to "01:00:00" ? Please help. Thanks in advance!
1
2766
by: Jason Chan | last post by:
DateTime mydate = new DateTime(2006,1,1,0,0,0); string testStr = mydate.ToString("hh:mm:ss"); //return 12:00:00 mydate = new DateTime(2006,1,1,1,0,0) testStr = mydate.ToString("hh:mm:ss"); //return 01:00:00 I want "00:00:00" instead of "12:00:00", what is going wrong?
5
19188
nirmalsingh
by: nirmalsingh | last post by:
i am getting date format in string as 30-11-2006 05:59:44 PM . i want to convert in the format of yyyy-mm-dd hh:mm:ss to sore it in mysql database. Help me with sample coding plz..
4
21333
sunbin
by: sunbin | last post by:
hi, How can i get the FLV file or any type of video file's duration in HH:MM:SS or HH:MM:SS:FF format from milliosecond ? I already got duration in millisecond. PHP script needed ... Plz help me ! Sunbin
1
3172
by: unknown66 | last post by:
hello, I have a time value in unix time that I would like to convert into a time format of hh:mm:ss:ms. For example, I have this number, 1172234138451, that I would like to be in the hh:mm:ss:ms format. I have tried timedelta as well as time.ctime. Does anyone have any suggestions? Thanks.
1
2687
seshu
by: seshu | last post by:
Hi every body to morning my cousine has show his application and also his live db in that to sav the length of all the voice files he has saved in time format ie he took the datatype of time now i want to generate a an excell sheet of that table but here i want the time to be like mm:ss but not hh:mm:ss examle the length of song sweet dreams is 01:12:35 now i want this to be shown as 72:35 how is there any procedure to convert directly in...
1
2169
seshu
by: seshu | last post by:
Hi every body to morning my cousine has show his application and also his live db in that to sav the length of all the voice files he has saved in time format ie he took the datatype of time now i want to generate a an excell sheet of that table but here i want the time to be like mm:ss but not hh:mm:ss examle the length of song sweet dreams is 01:12:35 now i want this to be shown as 72:35 how is there any procedure to convert directly in...
0
10341
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9954
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.