用PHP从天数生成计划日历

  • 本文关键字:计划 日历 PHP php
  • 更新时间 :
  • 英文 :


我正在尝试制作一个小程序来生成一个计划因此,用户选择:

  • 每天一天或多天(例如:选择周一、周二和周五(和小时
  • 输入starDate
  • 输入结束日期
  • 输入总小时数

之后,生成计划

从我现在的情况来看,我陷入了困境,因为它生成了一个全天的计划

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类来表示这一天的确切日期。

最新更新