首页 > 解决方案 > Get finish date, given start date and schedule in week

问题描述

I have a problem using PHP to get the finish date of course, as below:

Inputs:

Output: The finish date of course (is 9/9/2019 with above e.g inputs).

One lesson is one day. Input fields from end user: Input fields from end user

Sorry for my bad English. Thank you very much!

标签: phpdatedayofweek

解决方案


Try this code, if I understand you correctly))

<?
$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;


推荐阅读