Sum up a series of time differences

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wbryan6
Forum Newbie
Posts: 22
Joined: Sat Feb 04, 2006 12:13 pm

Sum up a series of time differences

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

What format are you times in? take a look at mktime() and unix timestamps. Adding and subtracting with timestamps is a cinch!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

mysql?? Where did he say anything about mysql?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

sorry... didn't mean for that to sound rude (I realize it came out that way)
Ward
Forum Commoner
Posts: 74
Joined: Thu Jul 13, 2006 10:01 am

Post 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.
wbryan6
Forum Newbie
Posts: 22
Joined: Sat Feb 04, 2006 12:13 pm

Post 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?
Post Reply