簡體   English   中英

將 MySQL 數據庫中的值顯示到日歷中

[英]Displaying values in a MySQL database into a calendar

我目前有一個 MySQL 數據庫,其中有保留。 它有 ArrivalDate 和 DepartureDate。 我正在尋找一種方法,讓它在他們逗留期間的所有日子里將每個預訂的姓名顯示到日歷中,以便輕松查看。 至少有一個關於如何解決這個問題的大綱將不勝感激。

謝謝!

目前,我有以下內容來計算他們逗留的所有天數:

$period = new DatePeriod(
    new DateTime($row['ArrivalDate']),
    DateInterval::createFromDateString('+1 day'),
    new DateTime($row['DepartureDate'])
);                    
foreach ( $period as $dt ) {
    echo $dt->format( 'd-m-Y' ) . "\n";
}

初始樣本數據:

+----+------------+-------------+---------------+
| id | name       | arrivalDate | departureDate |
+----+------------+-------------+---------------+
|  1 | John Smith | 2020-07-17  | 2020-07-20    |
|  2 | John Doe   | 2020-07-18  | 2020-07-22    |
|  3 | Jack Smith | 2020-07-28  | 2020-08-02    |
+----+------------+-------------+---------------+
3 rows in set (0,00 sec)

大綱是:您可能可以每月在單個 sql 查詢中執行此操作以獲得所需的數據,但我采用了不同的方法。 在 php 中獲取您要顯示的月份(如果未提供,則為當前月份)。 計算那個月的第一天和最后一天 - 做一個周期。 對於該期間的每一天,獲取當天有效的預訂。 現在,您擁有該月的所有日期以及每天的所有預訂,您可以根據需要顯示它。 還計算上個月和下個月並顯示此信息,以便您可以瀏覽下個月/上個月。

我把它作為一個接受一個月作為參數的腳本來做,如果沒有提供參數,它會顯示當前月份。

<?php

// database and query
$db = new PDO("mysql:host=localhost;dbname=calendar", 'z', 'z');

function selectFromDate(DateTime $date): array {
  global $db;

  $q = $db->prepare("SELECT * FROM reservations WHERE arrivalDate <= :date AND departureDate >= :date");
  $q->execute([':date' => $date->format('Y-m-d')]);

  return $q->fetchAll(PDO::FETCH_OBJ);
}

// initial dates
$yearMonthDate = new DateTime();
if ($argc > 1) {
  $yearMonthDate = new DateTime($argv[1]);
}
$yearMonth = $yearMonthDate->format('Y-m');
$nextYearMonth = (clone $yearMonthDate)->modify('first day of next month')->format('Y-m');
$previousYearMonth = (clone $yearMonthDate)->modify('first day of previous month')->format('Y-m');
$firstDayDate = (clone $yearMonthDate)->modify('first day of this month');
$lastDayDate = (clone $yearMonthDate)->modify('last day of this month');
$monthPeriod = new DatePeriod($firstDayDate, new DateInterval('P1D'), $lastDayDate);

// fetch data
$monthData = [];
foreach ($monthPeriod as $date) {
  $dayInfo = new stdClass();
  $dayInfo->date = $date->format('Y-m-d');
  $dayInfo->reservations = [];

  foreach (selectFromDate($date) as $reservation) {
    $dayInfo->reservations[] = $reservation->name;
  }
  $monthData[] = $dayInfo;
}
var_dump($monthData);

// display
echo 'Displaying: '.$yearMonth.PHP_EOL;
foreach ($monthData as $day) {
  echo '  '.$day->date.': '.join($day->reservations, ', ');
  echo PHP_EOL;
}

echo 'Previous month: '.$previousYearMonth. ', next month: '.$nextYearMonth.PHP_EOL;

你這樣稱呼它: file.php並像這樣得到 output (沒有參數 - 當月的 output ):

Displaying: 2020-07
  2020-07-01: 
  2020-07-02: 
  2020-07-03: 
  2020-07-04: 
  2020-07-05: 
  2020-07-06: 
  2020-07-07: 
  2020-07-08: 
  2020-07-09: 
  2020-07-10: 
  2020-07-11: 
  2020-07-12: 
  2020-07-13: 
  2020-07-14: 
  2020-07-15: 
  2020-07-16: 
  2020-07-17: John Smith
  2020-07-18: John Smith, John Doe
  2020-07-19: John Smith, John Doe
  2020-07-20: John Smith, John Doe
  2020-07-21: John Doe
  2020-07-22: John Doe
  2020-07-23: 
  2020-07-24: 
  2020-07-25: 
  2020-07-26: 
  2020-07-27: 
  2020-07-28: Jack Smith
  2020-07-29: Jack Smith
  2020-07-30: Jack Smith
Previous month: 2020-06, next month: 2020-08

$monthData的內容:

array(30) {
  [0]=>
  object(stdClass)#8 (2) {
    ["date"]=>
    string(10) "2020-07-01"
    ["reservations"]=>
    array(0) {
    }
  }
  [1]=>
  object(stdClass)#7 (2) {
    ["date"]=>
    string(10) "2020-07-02"
    ["reservations"]=>
    array(0) {
    }
  }
  [2]=>
  object(stdClass)#9 (2) {
    ["date"]=>
    string(10) "2020-07-03"
    ["reservations"]=>
    array(0) {
    }
  }
  [3]=>
  object(stdClass)#10 (2) {
    ["date"]=>
    string(10) "2020-07-04"
    ["reservations"]=>
    array(0) {
    }
  }
  [4]=>
  object(stdClass)#11 (2) {
    ["date"]=>
    string(10) "2020-07-05"
    ["reservations"]=>
    array(0) {
    }
  }
  [5]=>
  object(stdClass)#12 (2) {
    ["date"]=>
    string(10) "2020-07-06"
    ["reservations"]=>
    array(0) {
    }
  }
  [6]=>
  object(stdClass)#13 (2) {
    ["date"]=>
    string(10) "2020-07-07"
    ["reservations"]=>
    array(0) {
    }
  }
  [7]=>
  object(stdClass)#14 (2) {
    ["date"]=>
    string(10) "2020-07-08"
    ["reservations"]=>
    array(0) {
    }
  }
  [8]=>
  object(stdClass)#15 (2) {
    ["date"]=>
    string(10) "2020-07-09"
    ["reservations"]=>
    array(0) {
    }
  }
  [9]=>
  object(stdClass)#16 (2) {
    ["date"]=>
    string(10) "2020-07-10"
    ["reservations"]=>
    array(0) {
    }
  }
  [10]=>
  object(stdClass)#17 (2) {
    ["date"]=>
    string(10) "2020-07-11"
    ["reservations"]=>
    array(0) {
    }
  }
  [11]=>
  object(stdClass)#18 (2) {
    ["date"]=>
    string(10) "2020-07-12"
    ["reservations"]=>
    array(0) {
    }
  }
  [12]=>
  object(stdClass)#19 (2) {
    ["date"]=>
    string(10) "2020-07-13"
    ["reservations"]=>
    array(0) {
    }
  }
  [13]=>
  object(stdClass)#20 (2) {
    ["date"]=>
    string(10) "2020-07-14"
    ["reservations"]=>
    array(0) {
    }
  }
  [14]=>
  object(stdClass)#21 (2) {
    ["date"]=>
    string(10) "2020-07-15"
    ["reservations"]=>
    array(0) {
    }
  }
  [15]=>
  object(stdClass)#22 (2) {
    ["date"]=>
    string(10) "2020-07-16"
    ["reservations"]=>
    array(0) {
    }
  }
  [16]=>
  object(stdClass)#23 (2) {
    ["date"]=>
    string(10) "2020-07-17"
    ["reservations"]=>
    array(1) {
      [0]=>
      string(10) "John Smith"
    }
  }
  [17]=>
  object(stdClass)#24 (2) {
    ["date"]=>
    string(10) "2020-07-18"
    ["reservations"]=>
    array(2) {
      [0]=>
      string(10) "John Smith"
      [1]=>
      string(8) "John Doe"
    }
  }
  [18]=>
  object(stdClass)#25 (2) {
    ["date"]=>
    string(10) "2020-07-19"
    ["reservations"]=>
    array(2) {
      [0]=>
      string(10) "John Smith"
      [1]=>
      string(8) "John Doe"
    }
  }
  [19]=>
  object(stdClass)#28 (2) {
    ["date"]=>
    string(10) "2020-07-20"
    ["reservations"]=>
    array(2) {
      [0]=>
      string(10) "John Smith"
      [1]=>
      string(8) "John Doe"
    }
  }
  [20]=>
  object(stdClass)#27 (2) {
    ["date"]=>
    string(10) "2020-07-21"
    ["reservations"]=>
    array(1) {
      [0]=>
      string(8) "John Doe"
    }
  }
  [21]=>
  object(stdClass)#26 (2) {
    ["date"]=>
    string(10) "2020-07-22"
    ["reservations"]=>
    array(1) {
      [0]=>
      string(8) "John Doe"
    }
  }
  [22]=>
  object(stdClass)#31 (2) {
    ["date"]=>
    string(10) "2020-07-23"
    ["reservations"]=>
    array(0) {
    }
  }
  [23]=>
  object(stdClass)#29 (2) {
    ["date"]=>
    string(10) "2020-07-24"
    ["reservations"]=>
    array(0) {
    }
  }
  [24]=>
  object(stdClass)#30 (2) {
    ["date"]=>
    string(10) "2020-07-25"
    ["reservations"]=>
    array(0) {
    }
  }
  [25]=>
  object(stdClass)#33 (2) {
    ["date"]=>
    string(10) "2020-07-26"
    ["reservations"]=>
    array(0) {
    }
  }
  [26]=>
  object(stdClass)#34 (2) {
    ["date"]=>
    string(10) "2020-07-27"
    ["reservations"]=>
    array(0) {
    }
  }
  [27]=>
  object(stdClass)#35 (2) {
    ["date"]=>
    string(10) "2020-07-28"
    ["reservations"]=>
    array(1) {
      [0]=>
      string(10) "Jack Smith"
    }
  }
  [28]=>
  object(stdClass)#36 (2) {
    ["date"]=>
    string(10) "2020-07-29"
    ["reservations"]=>
    array(1) {
      [0]=>
      string(10) "Jack Smith"
    }
  }
  [29]=>
  object(stdClass)#32 (2) {
    ["date"]=>
    string(10) "2020-07-30"
    ["reservations"]=>
    array(1) {
      [0]=>
      string(10) "Jack Smith"
    }
  }
}

如您所見,它是一個包含 30 個元素的數組(一個月中的每一天)。 每個元素都包含這一天的日期和預訂,因此現在可以輕松地以您認為合適的方式顯示它(例如 html)。

如果你想在下個月看到這樣的調用: file.php 2020-08 ,你會得到這個 output:

Displaying: 2020-08
  2020-08-01: Jack Smith
  2020-08-02: Jack Smith
  2020-08-03: 
  2020-08-04: 
  2020-08-05: 
  2020-08-06: 
  2020-08-07: 
  2020-08-08: 
  2020-08-09: 
  2020-08-10: 
  2020-08-11: 
  2020-08-12: 
  2020-08-13: 
  2020-08-14: 
  2020-08-15: 
  2020-08-16: 
  2020-08-17: 
  2020-08-18: 
  2020-08-19: 
  2020-08-20: 
  2020-08-21: 
  2020-08-22: 
  2020-08-23: 
  2020-08-24: 
  2020-08-25: 
  2020-08-26: 
  2020-08-27: 
  2020-08-28: 
  2020-08-29: 
  2020-08-30: 
Previous month: 2020-07, next month: 2020-09

如果您想要瀏覽器版本,只需通過鏈接參數傳遞YYYY-MM參數。 如果要從日歷中排除出發日期,請修改查詢departureDate >:date

暫無
暫無

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

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