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.
Sum up a series of time differences
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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:
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.
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;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?