【问题标题】:Get finish date, given start date and schedule in week获取完成日期,给定的开始日期和一周内的时间表
【发布时间】:2019-12-29 18:10:45
【问题描述】:

当然,我在使用 PHP 获取完成日期时遇到问题,如下所示:

输入:

  • 开始日期(例如:8/23/2019
  • 按周安排(例如:Monday, Tuesday, Friday
  • 课程总数(例如:8

输出:课程结束日期(是9/9/2019,上面有例如输入)。

一天一课。来自最终用户的输入字段:

对不起,我的英语不好。非常感谢!

【问题讨论】:

  • 用户端输入了哪些字段?????
  • @MohitKumar,我刚刚更新了问题中最终用户的输入字段

标签: php date dayofweek


【解决方案1】:

我看到它在剩余课程时循环,并在星期一、星期二或星期五时减去。
循环后输出日期。

$start = "8/23/2019";
$days = ["Monday", "Tuesday", "Friday"];
$n = 8;

$d = strtotime($start);
while($n>0){
    //See if day is in days array
    if(in_array(date("l", $d), $days)){
        $n--;
    }
    $d += 86400; // go to next day
}

echo date("m/d/Y", $d-86400); //-86400 because  the loop adds one at the end.

【讨论】:

  • 哦,很短很清楚。我认为这是最好的解决方案
【解决方案2】:

试试这个代码,如果我理解正确的话))

<?
$startDay = '2019-09-25';
$aSchedule = array(1,2,4);
$iCntShed = count($aSchedule);
$iLessonsCnt = 8;
$iDWStartDay = date('w',strtotime($startDay));
$aDOWMap = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');

// first day according Schedule array and startDay
$aWeekDays = array_filter($aSchedule,function($iSDW) use ($iDWStartDay){
    return $iSDW>= $iDWStartDay;
});

// day according of week
$nextDate = date('Y-m-d',strtotime($startDay.' next '.$aDOWMap[end($aWeekDays)]));
$i = 0;

while (count($aWeekDays)<$iLessonsCnt) {
    $i = $i<$iCntShed ? $i : 0;
    $aWeekDays[] = $aSchedule[$i];
    $nextDate = date('Y-m-d',strtotime($nextDate.' next '.$aDOWMap[$aSchedule[$i++]]));
}

print_r($aWeekDays);

echo $nextDate;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 2021-02-16
    相关资源
    最近更新 更多