簡體   English   中英

PHP:在日期更改時增加變量

[英]PHP: Increment variable on date change

我正在構建一個課程安排工具,用戶可以在其中上傳 .csv 文件。 我想用一個變量來跟蹤日期,這樣用戶就可以上傳一個時間表,例如從一個月的 15 號開始。 因此,我想在日期更改時增加一個變量。 我從 csv 收到的數據如下所示:

array(
  array(
    '1',
    '2019-10-15',
    'Orientation / Team Building',
    '371',
    '',
    '0',
    '',
  ),
  array(
    '1',
    '2019-10-16',
    'Study Skills',
    '371',
    '',
    '0',
    '',
  ),
);

我想一次 output :

$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";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM