/** Calculate working days class**/
class calculateWorkingDays
{
private $holidays = array();
private $holidates = array();
private $totalWorkingDays = 0;
/**
* Constructor
*/
public function __construct() {
date_default_timezone_set(“UTC”);
$this->totalWorkingDays = 0;
}
/**
* Method to set Holidays i.e. Saturday, Sunday etc
* @param array $daysArray Array of Day Names which will be set as Holidays
*/
public function setHoliday($daysArray) {
$this->holidays = $daysArray;
}
/**
* Method to set specific Dates as Holidays
* @param array $datesArray Array of specific Dates which will be considered as Holidays
*/
public function setHolidates($datesArray) {
foreach ($datesArray as $k=>$v) {
$datesArray[$k] = strtotime($v);
}
$this->holidates = $datesArray;
}
/**
* Method to Calculate Total Working Days in a Date Range
* @param date $startDate
* @param date $endDate
*/
public function calculateDays($startDate, $endDate) {
$dtInfo = date_parse($startDate);
if ($dtInfo[‘warning_count’] != 0 || $dtInfo[‘error_count’] != 0) {
return ‘Start Date is not valid’;
}
$dtInfo = date_parse($endDate);
if ($dtInfo[‘warning_count’] != 0 || $dtInfo[‘error_count’] != 0) {
return ‘End Date is not valid’;
}
$diff = floor((strtotime($endDate)-strtotime($startDate))/(60*60*24));
$checkDate = $startDate;
for($i=1;$i<=$diff;$i++) {
if (count($this->holidays) > 0) {
if (!in_array(date(‘l’, strtotime($checkDate)), $this->holidays)) {
if (count($this->holidates) > 0) {
if (!in_array(strtotime($checkDate), $this->holidates)) {
$this->totalWorkingDays += 1;
}
} else {
$this->totalWorkingDays += 1;
}
}
} elseif (count($this->holidates) > 0) {
if (!in_array(strtotime($checkDate), $this->holidates)) {
$this->totalWorkingDays += 1;
}
} else {
$this->totalWorkingDays += 1;
}
$checkDate = date(“d-m-Y”, (strtotime($startDate)+($i*24*60*60)));
}
return $this->totalWorkingDays;
}
}
/** example for using class***/
$totWorkingDays = new calculateWorkingDays();
//set Holiday by its name
$totWorkingDays->setHoliday(array(‘Saturday’,’Sunday’));
//set Dates as Holiday
$totWorkingDays->setHolidate(array(’20 Dec 2014′, ‘2014-12-21’, ‘2014-12-25’));
//Calculate to total working days
echo $totWorkingDays->calculateDays(“2014-12-20”, “2015-12-30″) . ” Working days.”;
- Jquery webcam plugin - June 19, 2016
- How To Add and Delete Users on a CentOSServer - June 5, 2016
- How To Set Up vsftpd on CentOS 6 - June 5, 2016