I need a method for creating an array of "business days" in PHP beteewn 2 dates.
For example:
getWorkingDays("2008-01-01","2009-06-30开发者_开发问答");
Will create an array like the following:
Array
(
[0] ="2008-01-01",
[1] ="2008-01-05",
[2] ="2008-01-06",
[3] ="2008-01-07",
[4] ="2008-01-08",
[5] ="2008-01-09",
[6] ="2008-01-12",
[7] ="2008-01-13",
[8] ="2008-01-14",
...
)
The simplest technique would be to create a loop from the starting date until the end date (you should covert both to timestamps via mktime and then add a day (86400 seconds) to increment). In the inner part of the loop, you'd convert each date into a unix timestamp (once again via mktime, etc.) and then use...
date('N', $dayStamp)
...to get the day of the week for the date in question.
As a basic implemention, this would look like:
<?php
function getWorkingDays($startDate, $endDate) {
$businessDays = array();
$businessDaysInWeek = range(1,5); // Only Monday to Friday
// Decompose the provided dates.
list($startYear, $startMonth, $startDay) = explode('-', $startDate);
list($endYear, $endMonth, $endDay) = explode('-', $endDate);
// Create our start and end timestamps.
$startStamp = mktime(1, 1, 1, $startMonth, $startDay, $startYear);
$endStamp = mktime(1, 1, 1, $endMonth, $endDay, $endYear);
// Check each day in turn.
for($loop=$startStamp; $loop<=$endStamp; $loop+=86400) {
if(in_array(date('N', $loop), $businessDaysInWeek)) {
// You'll also want to omit bank holidays, etc. in here.
$businessDays[] = date('Y-m-d', $loop);
}
}
return $businessDays;
}
print_r(getWorkingDays('2011-01-10', '2011-01-24'));
?>
However, you'll most likely also need to omit bank holidays, etc. so you should store such exceptions in a database table/lookup array, etc. as check against them as appropriate.
Incidentally, if you're using a version of PHP earlier than 5.1.0, you should use date('w'...
and adjust your days of week accordingly. (It counts from zero instead of one.)
With the basics php date function you can make it, take the time to read the documentation at PHP date
精彩评论