Page 1 of 1

Sum up a series of time differences

Posted: Tue Jul 18, 2006 4:45 pm
by wbryan6
2 questions in this one:

1) I need to know the proper tactic for subtracting on time from another (eg. $end_time - $end_time = $value).

2) Once the arithmetic is done, I need to add up a series of time differences. Basically, I'm building a timesheet page and I need to total up the number of hours a person worked during the week, as well as provide daily totals.


Hopefully that is detailed enough to get the point across. Thanks in advance.

Posted: Tue Jul 18, 2006 5:06 pm
by Luke
What format are you times in? take a look at mktime() and unix timestamps. Adding and subtracting with timestamps is a cinch!

Posted: Tue Jul 18, 2006 6:45 pm
by John Cartwright
If your going to have dynamic calculations of times, I always prefer to use the internal date time functions of mysql handle the calculations for me.

Posted: Tue Jul 18, 2006 6:48 pm
by Luke
mysql?? Where did he say anything about mysql?

Posted: Tue Jul 18, 2006 6:50 pm
by John Cartwright
The Ninja Space Goat wrote:mysql?? Where did he say anything about mysql?
:?

I assume the data comes from somewhere, so I was throwing an idea of allowing a database do the work for you.

Posted: Tue Jul 18, 2006 6:53 pm
by Luke
sorry... didn't mean for that to sound rude (I realize it came out that way)

Posted: Tue Jul 18, 2006 9:50 pm
by Ward
unix timestamps are simplest to add/subtract, since they will always be an integer. BTW, a unix timestamp is simply the number of seconds passed since January 1st, 1970. You can convert to and from timestamp easily with php, using the date() and strtotime() functions.

Here is an example:

Code: Select all

// set start and end dates, standard MySQL DATE field format. 
// the strtotime() function will accept most any format, so these could be formatted differently
$start_date = "2006-01-01";
$end_date = "2006-06-01";

// convert dates to unix timestamps
$start_date_timestamp = strtotime($start_date);
$end_date_timestamp = strtotime($end_date);

// find the difference in seconds
$time_difference = $end_date_timestamp - $start_date_timestamp;

// minutes = seconds / 60
$time_difference_minutes = $time_difference / 60;

// hours = seconds / 60 / 60
$time_difference_hours = $time_difference / 60 / 60;

// days = seconds / 60 / 60 / 24
$time_difference_days = $time_difference / 60 / 60 / 24;

// weeks = seconds / 60 / 60 / 24 / 7
$time_difference_weeks = $time_difference / 60 / 60 / 24 / 7;
Months gets a little more difficult, since there aren't always 30 days in a month. Still, this should help you understand how you can find date differences with unix timestamps.

Posted: Wed Jul 19, 2006 12:10 pm
by wbryan6
Ok, I get how to find the differnce between two unix timestamps, but now how can I add all of them up? I am using Oralce to store time information for every day of the week. What I need is to total up the difference between sign in on Monday, signout on Monday, sign in on Tuesday, and so. I suppose the easiest way to say this is how can I store the time differnces and then call up their sum?