<?php
define('DB_SERVER', "localhost");
define('DB_USER', "certified_eh");
define('DB_PASSWORD', "why?");
define('DB_DATABASE', "lindelan_101");
define('DB_DRIVER', "mysql");
class BusinessDaysCalculator {
const MONDAY = 1;
const TUESDAY = 2;
const WEDNESDAY = 3;
const THURSDAY = 4;
const FRIDAY = 5;
const SATURDAY = 6;
const SUNDAY = 7;
/**
* @param DateTime $startDate Date to start calculations from
* @param DateTime[] $holidays Array of holidays, holidays are no conisdered business days.
* @param int[] $nonBusinessDays Array of days of the week which are not business days (Sat & Sun).
*/
public function __construct(DateTime $startDate, array $holidays, array $nonBusinessDays) {
$this->date = $startDate;
$this->holidays = $holidays;
$this->nonBusinessDays = $nonBusinessDays;
}
public function minusBusinessDays($howManyDays) {
$i = 0;
while ($i < $howManyDays) {
$this->date->modify("-1 day");
if ($this->isBusinessDay($this->date)) {
$i++;
}
}
}
public function getDate() {
return $this->date;
}
private function isBusinessDay(DateTime $date) {
if (in_array((int)$date->format('N'), $this->nonBusinessDays)) {
return false; //Date is a nonBusinessDay.
}
foreach ($this->holidays as $day) {
if ($date->format('Y-m-d') == $day->format('Y-m-d')) {
return false; //Date is a holiday.
}
}
return true; //Date is a business day.
}
public function getDays() {
return $this->date;
}
}
// Get Normal Days Difference, reverse with business days. Boom Client is happy...
try {
$db = new PDO(DB_DRIVER . ":dbname=" . DB_DATABASE . ";host=" . DB_SERVER, DB_USER, DB_PASSWORD);
$stmt = $db->prepare("SELECT help_id,DATEDIFF(CURDATE(),STR_TO_DATE(approvaldate, '%Y-%m-%d')) AS NormalDays FROM help");
$stmt->execute();
foreach( $stmt as $row )
{
$days = $row['NormalDays'];
//inst BusinessDaysCalculator class, send days, get date.
$calculator = new BusinessDaysCalculator(
new DateTime(), // Today
[new DateTime("2015-09-25")], // Existing holiday within expect range
[BusinessDaysCalculator::SUNDAY, BusinessDaysCalculator::SATURDAY]);
// Reverse
$calculator->minusBusinessDays($days);
//New Date
$reversedDate =$calculator->getDays();
$newDate = $reversedDate->format('Y-m-d');
if ($days>0){
$sql = "UPDATE `help`
SET `approvaldate` = :newDate
WHERE `help_id` = :help_id";
$statement = $db->prepare($sql);
$statement->bindValue(":help_id", $row['help_id']);
$statement->bindValue(":newDate", $newDate);
$statement->execute();
echo "One Victim Down.";
}
}
$db = null;
} catch(PDOException $e) {
trigger_error('Your IQ is 122 for a reason, Fix this buddy:' . $e->getMessage(), E_USER_ERROR);
}