note 52321 added to function.date

From: Date: Wed, 27 Apr 2005 20:03:56 +0000
Subject: note 52321 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-88657@lists.php.net to get a copy of this message
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; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.date.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52321 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52321&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52321&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52321&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52321&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52321&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52321&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#88657) next »