php - 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:
- Start date (e.g:
8/23/2019
) - Schedule in week (e.g:
Monday, Tuesday, Friday
) - Total of lessons (e.g:
8
)
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:
Sorry for my bad English. Thank you very much!
解决方案
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;
推荐阅读
- sql - 按每个月的最后一天分组——低效运行
- scala - 在 scala 无形库中,是否有一种将产品类型(HList/Generic/NamedGeneric)转换为与其数量相等的 int 单例类型的本地方法?
- scala - Circe 无法解码包含列表的 JSON
- cron - crontab -e 不工作。但是 crontab -l 有效
- excel - 将下拉列表添加到表格列中的所有单元格
- r - 有条件地更改R中一行中的单元格
- python - 带有 Python Selenium 的 WhatsApp Web 自动化(无法定位元素)
- python - 通过链接 str.replace 方法替换字符会产生错误的结果
- python - 如何为这个 python 函数编写测试?
- vb.net - 在我有几个函数参数的情况下,如何在 VB.NET 中传递一个包含所有参数的列表?