Reputation:
I'm trying to make a small program to generate a sheduler plan So, the user choose:
After that, the sheduler plan is generated
From what i have right now, i'm stuck in the foreach because it generate a plan for all days
function isTuesday($date) {
return date('w', strtotime($date)) === '2';
}
function isWednesday($date) {
return date('w', strtotime($date)) === '3';
}
foreach(range(0,365) as $day) {
$internal_date = date(INTERNAL_FORMAT, strtotime("{$start_date} + {$day} days"));
$this_day = date(DISPLAY_DAY_FORMAT, strtotime($internal_date));
$this_month = date(INTERNAL_FORMAT, strtotime($internal_date));
if ((isTuesday($internal_date) || isWednesday($internal_date))
&& !isExcludedDate($internal_date)) {
$months_and_dates[$this_month][] = $this_day;
}
}
It generates all dates from A to B dates, every Tuesday and Wednesday let suppose i use an if statement to check if every days week are selected? if monday, only call monday function if monday and tuesday, monday and tuesday function If i follow this i will have more than 30 if to cover all possibilities, any way making this shorter?
Thanks
UPDATE 1
If i use if, just for monday i have all this
foreach(range(0,$datediff) as $day) {
$internal_date = date(INTERNAL_FORMAT, strtotime("{$startDate} + {$day} days"));
$this_day = date(DISPLAY_DAY_FORMAT, strtotime($internal_date));
$this_month = date(INTERNAL_FORMAT, strtotime($internal_date));
if($isSegunda != null ){
if ((isSegunda($internal_date)) && !isExcludedDate($internal_date)) {
$cronograma[$this_month][] = $this_day;
}
}
if($isSegunda != null && $isTerca != null){
if ((isSegunda($internal_date)) || isTerca($internal_date) && !isExcludedDate($internal_date)) {
$cronograma[$this_month][] = $this_day;
}
}
if($isSegunda != null && $isTerca != null && $isQuarta != null){
if ((isSegunda($internal_date)) || isTerca($internal_date) || isQuarta($internal_date) && !isExcludedDate($internal_date)) {
$cronograma[$this_month][] = $this_day;
}
}
if($isSegunda != null && $isTerca != null && $isQuarta != null && $isQuinta != null){
if ((isSegunda($internal_date)) || isTerca($internal_date) || isQuarta($internal_date) || isQuinta($internal_date) && !isExcludedDate($internal_date)) {
$cronograma[$this_month][] = $this_day;
}
}
if($isSegunda != null && $isTerca != null && $isQuarta != null && $isQuinta != null && $isSexta !=null){
if ((isSegunda($internal_date)) || isTerca($internal_date) || isQuarta($internal_date) || isQuinta($internal_date) || isSexta($internal_date) && !isExcludedDate($internal_date)) {
$cronograma[$this_month][] = $this_day;
}
}
}
Upvotes: 0
Views: 47
Reputation: 514
Okay I created your scheduler rather the more readable way using classes
<?php
class Day
{
private $free;
private $workingHours;
private $name;
function __construct(string $name, bool $free, $workingHours = 0)
{
$this->name = $name;
$this->free = $free;
$this->workingHours = $workingHours;
}
public function setWorkingHours(int $workingHours)
{
$this->workingHours = $workingHours;
}
public function getWorkingHours(): int
{
return $this->workingHours;
}
public function isFree(): bool
{
return $this->free;
}
public function __toString()
{
return $this->name;
}
}
class Scheduler
{
const MONDAY = 'mon';
const TUESDAY = 'tue';
const WEDNESDAY = 'wed';
const THURSDAY = 'thu';
const FRIDAY = 'fri';
const SATURDAY = 'sat';
const SUNDAY = 'sun';
public function createSchedule(\DateTime $startDate, \DateTime $endDate, int $totalHours, array $scheduledWeekDays): array
{
$schedule = [];
$totalDays = 0;
// Find all scheduled days
while($startDate < $endDate) {
$weekday = strtolower($startDate->format('D'));
if ($this->isScheduledDay($weekday, $scheduledWeekDays)) {
$schedule[] = new Day($weekday, false);
$totalDays++;
} else {
$schedule[] = new Day($weekday, true);
}
$startDate->modify('+1days');
}
// Spread total hours evenly
$hoursPerDay = floor($totalHours / $totalDays); // round it down
$extraHours = $totalHours - ($hoursPerDay * $totalDays); // find the hours that were left by flooring
$schedule = array_map(function(Day $day) use ($hoursPerDay) {
if(!$day->isFree()) {
$day->setWorkingHours($hoursPerDay);
}
return $day;
}, $schedule);
$lastDay = $schedule[count($schedule) - 1];
$lastDay->setWorkingHours($lastDay->getWorkingHours() + $extraHours);
return $schedule;
}
private function isScheduledDay(string $day, array $scheduledWeekDays)
{
return array_search($day, $scheduledWeekDays) !== false;
}
}
$scheduler = new Scheduler();
$startDate = new \DateTime('tomorrow');
$endDate = (clone $startDate)->modify('+1week');
$schedule = $scheduler->createSchedule($startDate, $endDate, 16, [Scheduler::SATURDAY, Scheduler::SUNDAY]);
// 8 hours on saturday, and sunday
foreach($schedule as $day) {
$text = 'Week day: ' . $day . '<br>';
if($day->isFree()) {
$text .= 'It\'s a free day enjoy ur time!';
} else {
$text .= 'Today you have to work ' . $day->getWorkingHours() . ' hours';
}
$text .= '<br><br>';
echo $text;
}
You can extend the day class for example to represent the exact date of this day.
Upvotes: 0