当前位置: 首页 > 知识库问答 >
问题:

用PHP从天生成sheduler日历

祁烈
2023-03-14

我正在尝试做一个小程序来生成一个舍杜勒计划,所以,用户选择:

  • 一天或多天(例如:选择星期一,星期二和星期五)和每天的小时
  • 输入起始日期
  • 输入结束日期
  • 输入TOTAhours

然后,生成下料计划

从我现在的情况来看,我被困在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;
    }
    }
    
    
    
}

共有1个答案

墨宜人
2023-03-14

好吧,我创建了您的日程安排程序,而不是更易读的方式使用类

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

 类似资料:
  • 问题内容: 我想运行一个查询 并返回如下数据: 问题答案: 此解决方案不使用 循环,过程或临时表 。子查询会生成最近10,000天的日期,并且可以扩展为任意向前或向后的日期。 输出: 性能说明 在这里对其进行测试,其性能令人惊讶地良好: 上面的查询花费了0.0009秒。 如果我们扩展子查询以生成近似值。100,000个数字(因此约有274年的日期),运行时间为0.0458秒。 顺便说一句,这是一种

  • 问题内容: 我想运行一个查询 并返回如下数据: 问题答案: 此解决方案不使用 循环,过程或临时表 。子查询会生成最近10,000天的日期,并且可以扩展为任意向前或向后的日期。 输出: 性能说明 在这里对其进行测试,其性能出奇地好: 上面的查询花费了0.0009秒。 如果我们扩展子查询以生成近似值。100,000个数字(因此约有274年的日期),运行时间为0.0458秒。 顺便说一句,这是一种非常可

  • 我想通过log4j2配置生成半天日志文件。我已经给出: 还定义了: 但我只得到AM日志在这种方式,虽然我希望它是什么: 有人能帮我吗?

  • 问题内容: 我有一个PHP日期格式,我想以相同格式获取明天日期,例如。 PHP怎么可能? 问题答案: 使用日期时间 要么: 要么: 或在PHP 5.4+中:

  • 问题内容: 我基本上是想将Unix时间戳(time()函数)转换为与过去和将来的日期都兼容的相对日期/时间。因此输出可能是: 2个星期前 1小时60分钟前 15分钟54秒前 10分钟15秒后 首先,我尝试编写此代码,但是做了一个无法维护的巨大功能,然后我在互联网上搜索了几个小时,但我所能找到的只是脚本仅产生一部分时间(例如:“ 1小时前”纪要)。 您是否已经有执行此操作的脚本? 问题答案: 此功能

  • 问题内容: 我正在使用php代码查询数据库,结果将用于生成报告。 如果我想以pdf格式生成报告,该怎么办? 问题答案: 如果您的PDF文件中需要UTF支持,请考虑使用库。 从此处下载:http : //www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf 在您的脚本中:

  • 问题内容: 我有一个PHP应用程序,需要用查询结果生成PDF。被发现的最简单的方法是使用DOMPDF为我生成PDF。因此,做了一个为我生成HTML的函数,然后将其传递给DOMPDF。在开发和测试环境中,一切都很好,但是在生产环境中,我在内存使用方面存在一些问题。 因此,我想知道我的策略是最好的还是有更好更好的方法来做到这一点。 你会怎么做? 问题答案: 我曾经做过一个生成PDF的PHP项目。我使用

  • 可以因此,我们都看到了如何从现在到将来的某个日期之间获得剩余天数,就像我在下面的简单代码中所做的那样: 说:$endate=一个未来的日期(从今天算起5天); 上面的PHP代码将告诉您现在和未来日期之间还剩下多少天。 但我想要的是这种情况的反面。那是。 而不是拥有 我需要像这样的东西