0

So I've been googling around the web trying to find some sort of PHP API that nicely handles the calculation of business days. I found a number of answers and code snippets on stackoverflow which were helpful, but the code I found that handled adding business days to a given date lacked certain features I need (e.g. handling holidays that fall on a weekend), and it was difficult to customize based on a given application's needs.

So I finally decided to bite the bullet and write my own solution. The class I've produced incorporates code that I found on stackoverflow (James Pasta++) as well as logic used in a similar (and simpler) Java class that I wrote last year. I'm posting it here Q/A style in case anyone else encounters the same frustrations that I've had with this concept, and finds it a useful solution.

There are two basic problems that I tackled - 1) adding business days to a provided date, and 2) counting the business days between two dates. My primary concerns in solving these issues were ease of use and extensibility; there are a number of places that were written less than efficiently; if it becomes an issue I can certainly refactor parts of the code, but at least for my purposes, I won't be calculating thousands of business days into the future :-)

Sample code and a link to the source can be found in the answer below. Feedback and improvements are more than welcome.

Community
  • 1
  • 1
opensourcejunkie
  • 518
  • 7
  • 14

1 Answers1

0

The Code

The basic concept of this library is that a date is wrapped with a BusinessDay object; it can then be compare()d with other dates, or it can have business days add()ed or subtract()ed. All dates provided to the library can be one of the following four types: a date string accepted by strtotime(), a UNIX timestamp, another BusinessDay object or null (represents the current time).

BusinessDay objects can be instantiated via a new statement, or by a function called wrapBusinessDay(). Using the latter option allows method chaining upon instantiation, which can help keep code succinct. See the sample code below.

Sample Code:

// add a business day to today
$ship_date = wrapBusinessDay(time())->add(1);

// find # of business days until required date
$required_date = '6/24/2013';
$difference = $ship_date->difference($required_date);

// panic?!
if ($difference < 10) {
    echo "Uh-oh, we'll need expedited shipping!\n";
} else {
    echo "Whew!  we're safe.\n";
}

// specify custom holidays
$arr_matey = wrapBusinessDay('2013-09-18', array(
    '2013-09-19'
))->add(1);

// specify custom formats
echo $arr_matey . "\n";                      // prints '2013-09-20 00:00:00'
echo $arr_matey->dateFormat('m~d~Y') . "\n"; // prints '09~20~2013'

There's more to the library, but that's the gist of it. Hope this'll save somebody else some time! ~ Nate

opensourcejunkie
  • 518
  • 7
  • 14
  • Very good.. but this solution does not account for holidays on weekends that are observed on weekdays...FYI – EGriff Dec 21 '13 at 17:38