Thursday, May 24, 2007

Get First Buisness day of Month- PHP

I have written a small function which calculates if given date is first buisness day of that month.
This can be set in cron job and run every day to check if this is first buisness day.
This can be useful for sending newsletters / promotional offers , etc on first buisness day of every month.

function definition is given below

* Function which checks if current date passed is a first buisness day of any month or NOT.

* Description - First it checks if given date is 01 if yes then check if its not sat/ sun / public holidays.
if 01st is nonbuisness day then ignore return false.
In this case when the function will be called again on 02, here it will check if 02 is buisness day, if yes then if 01 was non buisness day then 02 is definitely first buisness day of the month.
This check will be done until 06 of every month.

* @param integer $year year in YYYY format
* @param integer $month Month in MM format
* @param integer $day day in DD format
* @param mixed $non_buisness_days array containing all non buisness days values will be in format Sat, Sun, Tue
* @param mixed $public_holidays An array containing list of all holidays for given year, the dates should be in YYYY-MM-DD format
* @return bool true if given date is first buisness day else otherwise false.
* @author VN (02/05/2007)

function is_first_buisness_day($year,$month,$day,$non_buisness_days='',$public_holidays='')
$tmpstamp = mktime(1,1,1,$month,$day,$year);
//echo $year,$month.$day."
$ymd = date('Y-m-d',$tmpstamp);

if($public_holidays == ''){
$public_holidays = array();
if($non_buisness_days == ''){
$non_buisness_days = array();

$d = date('d',$tmpstamp);
$day = date('D',$tmpstamp);
$month = date('m',$tmpstamp);
$year = date('Y',$tmpstamp);

$previous_d = $d-1;
$previous_day = date('D',mktime(1,1,1,$month,$previous_d,$year));

$tp_first_day = mktime(1,1,1,$month,01,$year);
$day_first_day = date('D',$tp_first_day);
$day_first_ymd = date('Y-m-d',$tp_first_day);

if(($d == 01 && (!in_array($day,$non_buisness_days) && !in_array($ymd,$public_holidays)))
($d != 01 && (in_array($day_first_day,$non_buisness_days) in_array($day_first_ymd,$public_holidays)) &&
(!in_array($day,$non_buisness_days)) && (!in_array($ymd,$public_holidays)) &&
($d <=5 && (in_array($previous_day,$non_buisness_days) (in_array($day_first_ymd,$public_holidays)))))){ $return_value = true; } else { $return_value = false; } unset($d); unset($day); unset($month); unset($year); unset($non_buisness_days); unset($previous_d); unset($previous_day); unset($tp_first_day); unset($day_first_day); unset($public_holidays); return $return_value; }


$pb = array('2007-05-01','2007-12-25');
$dt = $_GET['getdate'];;
$dt = explode('-',$dt);

$non_buisness_days = array('Sat','Sun');

echo "Non Buisness days array:
foreach($non_buisness_days as $i => $v){
echo "'".$v."'";
echo " ";
echo "

echo "Public holiday array:
foreach($pb as $i => $v){
echo $v;
echo "
echo "


$bool = is_first_buisness_day($dt[0],$dt[1],$dt[2],$non_buisness_days,$pb);

echo 'YES
'.$_GET['getdate'].' is a first buisness day of Month '.date('F - Y',mktime(1,1,1,$dt[1],$dt[2],$dt[0]));
echo 'NO
'.$_GET['getdate'].' is not a first buisness day of Month '.date('F - Y',mktime(1,1,1,$dt[1],$dt[2],$dt[0]));

No comments: