我正在尝试做一个小程序来生成一个舍杜勒计划,所以,用户选择:
然后,生成下料计划
从我现在的情况来看,我被困在foreach里,因为它产生了一个所有日子的计划
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;
}
}
它生成从A到B的所有日期,每个星期二和星期三。假设我使用一个if语句来检查是否选择了“每一天”和“每一周”? 如果星期一,只调用星期一函数如果星期一和星期二,星期一和星期二函数如果我遵循这个我将有超过30如果涵盖所有的可能性,任何方式使这更短?
谢谢
更新1
如果我用If,就为了星期一我有这些
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;
}
}
}
好吧,我创建了您的日程安排程序,而不是更易读的方式使用类
<?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;
}
例如,您可以扩展day类以表示这一天的确切日期。