note 52321 deleted from function.date by derick
| From: | derick@php.net | Date: | Sun, 15 Jan 2006 12:45:37 +0000 |
| Subject: | note 52321 deleted from function.date by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-101837@lists.php.net to get a copy of this message | ||
Note Submitter: paul_liversidge at hotmail dot com
----
A quick function to calculate the number of working days between two dates in YYYY-MM-DD format
*without* iterating through every day and comparing it to the weekday. The start and end dates are
included in the calculation. I don't know how to easily explain the final calculation, I just
happened to notice the pattern in the test data and this is how I ended up expressing it. :)
<?php
function CalcWorkingDays ($start, $end) {
$starttime = gmmktime (0, 0, 0, substr ($start, 5, 2), substr ($start, 8, 2), substr ($start, 0,
4));
$endtime = gmmktime (0, 0, 0, substr ($end, 5, 2), substr ($end, 8, 2), substr ($end, 0, 4));
$days = (($endtime - $starttime) / 86400) + 1;
$d = $days % 7;
$w = date("w", $starttime);
$result = floor ($days / 7) * 5;
$result = $result + $d - (($d + $w) >= 7) - (($d + $w) >= 8) - ($w == 0);
return $result;
}
?>