Last active
August 29, 2015 14:02
-
-
Save Isinlor/308a745a1aa1524072a9 to your computer and use it in GitHub Desktop.
workingTime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class workingTime { | |
public $start = 9; | |
public $end = 17; // 17:XX so also 17:59 | |
public $freeWeekDays = array(0 => true, | |
6 => true); // week 0-6; true = free; | |
public $freeYearDays = []; // year 0-365; true = free; | |
public function __construct(array $config = []){ | |
foreach($config as $setting => $value){ | |
$this->$setting = $value; | |
} | |
} | |
public function isItWorkingTime($time){ | |
if($this->isItWorkingHour($time) && $this->isItWorkingDay($time)){ | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public function isItWorkingHour($time){ | |
$hour = date('G', $time); // 0-23 format | |
if($hour >= $this->start && $hour <= $this->end){ | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public function isItWorkingDay($time){ | |
$weekDay = date('w', $time); // 0-6 format | |
$yearDay = date('z', $time); // 0-365 format | |
$isItFreeWeekDay = isset($this->freeWeekDays[$weekDay]) && $this->freeWeekDays[$weekDay]; | |
$isItFreeYearDay = isset($this->freeYearDays[$yearDay]) && $this->freeYearDays[$yearDay]; | |
return !$isItFreeWeekDay && !$isItFreeYearDay; | |
} | |
public function nextWorkingDay($time){ | |
$tomorrow = strtotime('tomorrow '.$this->start.' hour', $time); | |
if($this->isItWorkingDay($tomorrow)){ | |
return $tomorrow; | |
} else{ | |
$nextMonday = strtotime('next monday '.$this->start.' hour', $time); | |
return $nextMonday; | |
} | |
} | |
} | |
class pretendWorking { | |
protected $workingTime; | |
public function __construct(workingTime $workingTime){ | |
$this -> workingTime = $workingTime; | |
} | |
public function whenToSend($whenRecived){ | |
$howLongPretend = $this->howLongPretend(); | |
$whenToSend = $whenRecived + $howLongPretend; | |
if($this->workingTime->isItWorkingTime($whenRecived)){ | |
if($this->workingTime->isItWorkingTime($whenToSend)){ | |
return $whenToSend; | |
} else { | |
$howLongPretended = strtotime('today '.($this->workingTime->end+1).' hour', $whenRecived) - $whenRecived; | |
return $this->workingTime->nextWorkingDay($whenRecived) + $howLongPretend - $howLongPretended; | |
} | |
} else { | |
return $this->workingTime->nextWorkingDay($whenRecived) + $howLongPretend; | |
} | |
} | |
// 4 h of pretending of working + 0-60 min random | |
protected function howLongPretend(){ | |
return 4*60*60 + rand(0, 60)*rand(0, 60); | |
} | |
} | |
//$now = time(); | |
$now = 1403880535; | |
echo 'It is: '.date(DATE_COOKIE, $now).'<br>'; | |
$workingTime = new workingTime(); | |
$pretendWorking = new pretendWorking($workingTime); | |
$whenToSend = $pretendWorking->whenToSend($now); | |
echo 'You should send this on: '.date(DATE_COOKIE, $whenToSend); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment