
August 5th, 2004, 07:52 AM
|
 |
Command Line Warrior
|
|
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
|
|
How about something like this?
PHP Code:
function workingdaycount($date1, $date2)
{
##### init result value
$workdays = 0;
##### get unix timestamps (input must be formatted yyyy-mm-dd)
$unixdate1 = mktime(0,0,0, intval(substr($date1, 5, 2)), intval(substr($date1, 8, 2)), intval(substr($date1, 0, 4)));
$unixdate2 = mktime(0,0,0, intval(substr($date2, 5, 2)), intval(substr($date2, 8, 2)), intval(substr($date2, 0, 4)));
##### make sure order of stamps is correct
if($unixdate2<$unixdate1)
{
$temp = $unixdate1;
$unixdate1 = $unixdate2;
$unixdate2 = $temp;
}
##### get time difference
$timediff = $unixdate2 - $unixdate1;
$weekseconds = 604800; # 60*60*24*7 = amount of seconds in a week
##### get number of complete weeks, multiply by 5 for number of working days.
$workdays += (5 * floor($timediff / $weekseconds));
##### get day of week for both entries (0 => sunday, 6 => saturday)
$weekday1 = date('w', $unixdate1);
$weekday2 = date('w', $unixdate2);
##### calculate amount of days in between, weekwise
if($weekday1 < $weekday2) # no weekend in between
{
if($weekday2 > 5)
$weekday2 = 5;
$workdays += $weekday2 - $weekday1;
} # weekend in between
else
$workdays += 5 + $weekday2 - $weekday1;
##### return result
return $workdays;
}
Of course, this can be shortened a lot, but wanted to be clear.
Hope this helps...
|