php - PHP:日期变化的增量变量
问题描述
我正在构建一个课程安排工具,用户可以在其中上传 .csv 文件。我想用一个变量来跟踪日期,这样用户就可以上传一个时间表,例如从一个月的 15 号开始。因此,我想在日期更改时增加一个变量。我从 csv 收到的数据如下所示:
array(
array(
'1',
'2019-10-15',
'Orientation / Team Building',
'371',
'',
'0',
'',
),
array(
'1',
'2019-10-16',
'Study Skills',
'371',
'',
'0',
'',
),
);
我想一次输出一天:
$currentMonth = date('F');
$dayOfSchedule = 0;
while ($dayOfSchedule < sizeof($schedule[$currentMonth]){
echo $schedule[$currentMonth][$dayOfSchedule][2]; //output the class etc.
}
然后在当天更改时增加 $dayOfSchedule ,但我不知道该怎么做。
任何帮助深表感谢!
解决方案
您可能正在寻找这样的东西:
<?php
$data = array(
array(
1,
'2019-10-16',
'Study Skills',
'371',
'',
0,
'',
),
array(
1,
'2019-10-16',
'Mathematics',
'371',
'',
0,
'',
),
array(
1,
'2019-10-15',
'Orientation / Team Building',
371,
'',
0,
'',
),
);
// map rows according to the date
$schedule = array_reduce($data, function ($accu, $row) {
$date = \DateTime::createFromFormat('Y-m-d', $row[1]);
$accu[$date->format('F')][$date->format('j')][] = $row;
return $accu;
}, []);
// sort the days and months
ksort($schedule);
array_walk($schedule, function (&$month_schedule) {
ksort($month_schedule);
});
$today = date('j'); // day of month today
if (isset($schedule[date('F')])) {
foreach ($schedule[date('F')] as $day => $rows) {
if ((int) $day >= (int) $today) {
echo " Day of Month: $day\n";
foreach ($rows as $key => $row) {
echo " Class({$key}): {$row[2]}\n";
}
}
}
echo "\n";
}
推荐阅读
- ios - 如何从麦克风快速(超声波)读取音频?
- java - 日志记录:将日志与正在创建的对象关联
- css - Flipclock 倒计时不会出现在 IE 的页面加载中
- reactjs - VS Code - 使用同一窗口打开文件并保留工作区
- css - CSS - 问题 - 小缩略图变得巨大 - 并阻塞整个显示
- javascript - 导出到excel时为表格添加标题
- linux - RHEL 上的 Kafka 给了我“类路径为空”
- python - python3自定义编码mik-bulgarian
- powerapps - 图库过滤器 - powerapps
- .net - NetCore 应用程序配置可以使用 Properties.Settings 覆盖类库中的值吗?