将mysql数据库中的值显示到日历中

rnmwe5a2  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(436)

我现在有一个mysql数据库,里面有预订。它有到达日期和离开日期。我正在寻找一种方法,让它显示从每个预订到日历中的名字,在他们逗留期间的所有日子,所以它可以很容易地查看。至少一个关于如何处理这一问题的提纲将不胜感激。
谢谢!
目前,我有以下数据来计算他们的所有逗留天数:

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

8oomwypt1#

初始样本数据:

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

概要是:您可以每月通过一次sql查询来获取所需的数据,但我采用了不同的方法。在php中,获取要显示的月份(如果未提供,则为当前月份)。算出那个月的第一天和最后一天——做一个句号。对于期间的每一天,获取当天有效的预订。现在您已经拥有了一个月中的所有日期以及每天的所有预订,您可以根据需要显示它。还可以计算上一个月和下一个月并显示此信息,以便您可以浏览下一个月/上一个月。
我是作为一个脚本来做的,它接受一个月作为参数,如果没有提供参数,它将显示当前月份。

  1. <?php
  2. // database and query
  3. $db = new PDO("mysql:host=localhost;dbname=calendar", 'z', 'z');
  4. function selectFromDate(DateTime $date): array {
  5. global $db;
  6. $q = $db->prepare("SELECT * FROM reservations WHERE arrivalDate <= :date AND departureDate >= :date");
  7. $q->execute([':date' => $date->format('Y-m-d')]);
  8. return $q->fetchAll(PDO::FETCH_OBJ);
  9. }
  10. // initial dates
  11. $yearMonthDate = new DateTime();
  12. if ($argc > 1) {
  13. $yearMonthDate = new DateTime($argv[1]);
  14. }
  15. $yearMonth = $yearMonthDate->format('Y-m');
  16. $nextYearMonth = (clone $yearMonthDate)->modify('first day of next month')->format('Y-m');
  17. $previousYearMonth = (clone $yearMonthDate)->modify('first day of previous month')->format('Y-m');
  18. $firstDayDate = (clone $yearMonthDate)->modify('first day of this month');
  19. $lastDayDate = (clone $yearMonthDate)->modify('last day of this month');
  20. $monthPeriod = new DatePeriod($firstDayDate, new DateInterval('P1D'), $lastDayDate);
  21. // fetch data
  22. $monthData = [];
  23. foreach ($monthPeriod as $date) {
  24. $dayInfo = new stdClass();
  25. $dayInfo->date = $date->format('Y-m-d');
  26. $dayInfo->reservations = [];
  27. foreach (selectFromDate($date) as $reservation) {
  28. $dayInfo->reservations[] = $reservation->name;
  29. }
  30. $monthData[] = $dayInfo;
  31. }
  32. var_dump($monthData);
  33. // display
  34. echo 'Displaying: '.$yearMonth.PHP_EOL;
  35. foreach ($monthData as $day) {
  36. echo ' '.$day->date.': '.join($day->reservations, ', ');
  37. echo PHP_EOL;
  38. }
  39. echo 'Previous month: '.$previousYearMonth. ', next month: '.$nextYearMonth.PHP_EOL;

你这样称呼它: file.php 并得到如下输出(无参数-当月输出):

  1. Displaying: 2020-07
  2. 2020-07-01:
  3. 2020-07-02:
  4. 2020-07-03:
  5. 2020-07-04:
  6. 2020-07-05:
  7. 2020-07-06:
  8. 2020-07-07:
  9. 2020-07-08:
  10. 2020-07-09:
  11. 2020-07-10:
  12. 2020-07-11:
  13. 2020-07-12:
  14. 2020-07-13:
  15. 2020-07-14:
  16. 2020-07-15:
  17. 2020-07-16:
  18. 2020-07-17: John Smith
  19. 2020-07-18: John Smith, John Doe
  20. 2020-07-19: John Smith, John Doe
  21. 2020-07-20: John Smith, John Doe
  22. 2020-07-21: John Doe
  23. 2020-07-22: John Doe
  24. 2020-07-23:
  25. 2020-07-24:
  26. 2020-07-25:
  27. 2020-07-26:
  28. 2020-07-27:
  29. 2020-07-28: Jack Smith
  30. 2020-07-29: Jack Smith
  31. 2020-07-30: Jack Smith
  32. Previous month: 2020-06, next month: 2020-08

的内容 $monthData :

  1. array(30) {
  2. [0]=>
  3. object(stdClass)#8 (2) {
  4. ["date"]=>
  5. string(10) "2020-07-01"
  6. ["reservations"]=>
  7. array(0) {
  8. }
  9. }
  10. [1]=>
  11. object(stdClass)#7 (2) {
  12. ["date"]=>
  13. string(10) "2020-07-02"
  14. ["reservations"]=>
  15. array(0) {
  16. }
  17. }
  18. [2]=>
  19. object(stdClass)#9 (2) {
  20. ["date"]=>
  21. string(10) "2020-07-03"
  22. ["reservations"]=>
  23. array(0) {
  24. }
  25. }
  26. [3]=>
  27. object(stdClass)#10 (2) {
  28. ["date"]=>
  29. string(10) "2020-07-04"
  30. ["reservations"]=>
  31. array(0) {
  32. }
  33. }
  34. [4]=>
  35. object(stdClass)#11 (2) {
  36. ["date"]=>
  37. string(10) "2020-07-05"
  38. ["reservations"]=>
  39. array(0) {
  40. }
  41. }
  42. [5]=>
  43. object(stdClass)#12 (2) {
  44. ["date"]=>
  45. string(10) "2020-07-06"
  46. ["reservations"]=>
  47. array(0) {
  48. }
  49. }
  50. [6]=>
  51. object(stdClass)#13 (2) {
  52. ["date"]=>
  53. string(10) "2020-07-07"
  54. ["reservations"]=>
  55. array(0) {
  56. }
  57. }
  58. [7]=>
  59. object(stdClass)#14 (2) {
  60. ["date"]=>
  61. string(10) "2020-07-08"
  62. ["reservations"]=>
  63. array(0) {
  64. }
  65. }
  66. [8]=>
  67. object(stdClass)#15 (2) {
  68. ["date"]=>
  69. string(10) "2020-07-09"
  70. ["reservations"]=>
  71. array(0) {
  72. }
  73. }
  74. [9]=>
  75. object(stdClass)#16 (2) {
  76. ["date"]=>
  77. string(10) "2020-07-10"
  78. ["reservations"]=>
  79. array(0) {
  80. }
  81. }
  82. [10]=>
  83. object(stdClass)#17 (2) {
  84. ["date"]=>
  85. string(10) "2020-07-11"
  86. ["reservations"]=>
  87. array(0) {
  88. }
  89. }
  90. [11]=>
  91. object(stdClass)#18 (2) {
  92. ["date"]=>
  93. string(10) "2020-07-12"
  94. ["reservations"]=>
  95. array(0) {
  96. }
  97. }
  98. [12]=>
  99. object(stdClass)#19 (2) {
  100. ["date"]=>
  101. string(10) "2020-07-13"
  102. ["reservations"]=>
  103. array(0) {
  104. }
  105. }
  106. [13]=>
  107. object(stdClass)#20 (2) {
  108. ["date"]=>
  109. string(10) "2020-07-14"
  110. ["reservations"]=>
  111. array(0) {
  112. }
  113. }
  114. [14]=>
  115. object(stdClass)#21 (2) {
  116. ["date"]=>
  117. string(10) "2020-07-15"
  118. ["reservations"]=>
  119. array(0) {
  120. }
  121. }
  122. [15]=>
  123. object(stdClass)#22 (2) {
  124. ["date"]=>
  125. string(10) "2020-07-16"
  126. ["reservations"]=>
  127. array(0) {
  128. }
  129. }
  130. [16]=>
  131. object(stdClass)#23 (2) {
  132. ["date"]=>
  133. string(10) "2020-07-17"
  134. ["reservations"]=>
  135. array(1) {
  136. [0]=>
  137. string(10) "John Smith"
  138. }
  139. }
  140. [17]=>
  141. object(stdClass)#24 (2) {
  142. ["date"]=>
  143. string(10) "2020-07-18"
  144. ["reservations"]=>
  145. array(2) {
  146. [0]=>
  147. string(10) "John Smith"
  148. [1]=>
  149. string(8) "John Doe"
  150. }
  151. }
  152. [18]=>
  153. object(stdClass)#25 (2) {
  154. ["date"]=>
  155. string(10) "2020-07-19"
  156. ["reservations"]=>
  157. array(2) {
  158. [0]=>
  159. string(10) "John Smith"
  160. [1]=>
  161. string(8) "John Doe"
  162. }
  163. }
  164. [19]=>
  165. object(stdClass)#28 (2) {
  166. ["date"]=>
  167. string(10) "2020-07-20"
  168. ["reservations"]=>
  169. array(2) {
  170. [0]=>
  171. string(10) "John Smith"
  172. [1]=>
  173. string(8) "John Doe"
  174. }
  175. }
  176. [20]=>
  177. object(stdClass)#27 (2) {
  178. ["date"]=>
  179. string(10) "2020-07-21"
  180. ["reservations"]=>
  181. array(1) {
  182. [0]=>
  183. string(8) "John Doe"
  184. }
  185. }
  186. [21]=>
  187. object(stdClass)#26 (2) {
  188. ["date"]=>
  189. string(10) "2020-07-22"
  190. ["reservations"]=>
  191. array(1) {
  192. [0]=>
  193. string(8) "John Doe"
  194. }
  195. }
  196. [22]=>
  197. object(stdClass)#31 (2) {
  198. ["date"]=>
  199. string(10) "2020-07-23"
  200. ["reservations"]=>
  201. array(0) {
  202. }
  203. }
  204. [23]=>
  205. object(stdClass)#29 (2) {
  206. ["date"]=>
  207. string(10) "2020-07-24"
  208. ["reservations"]=>
  209. array(0) {
  210. }
  211. }
  212. [24]=>
  213. object(stdClass)#30 (2) {
  214. ["date"]=>
  215. string(10) "2020-07-25"
  216. ["reservations"]=>
  217. array(0) {
  218. }
  219. }
  220. [25]=>
  221. object(stdClass)#33 (2) {
  222. ["date"]=>
  223. string(10) "2020-07-26"
  224. ["reservations"]=>
  225. array(0) {
  226. }
  227. }
  228. [26]=>
  229. object(stdClass)#34 (2) {
  230. ["date"]=>
  231. string(10) "2020-07-27"
  232. ["reservations"]=>
  233. array(0) {
  234. }
  235. }
  236. [27]=>
  237. object(stdClass)#35 (2) {
  238. ["date"]=>
  239. string(10) "2020-07-28"
  240. ["reservations"]=>
  241. array(1) {
  242. [0]=>
  243. string(10) "Jack Smith"
  244. }
  245. }
  246. [28]=>
  247. object(stdClass)#36 (2) {
  248. ["date"]=>
  249. string(10) "2020-07-29"
  250. ["reservations"]=>
  251. array(1) {
  252. [0]=>
  253. string(10) "Jack Smith"
  254. }
  255. }
  256. [29]=>
  257. object(stdClass)#32 (2) {
  258. ["date"]=>
  259. string(10) "2020-07-30"
  260. ["reservations"]=>
  261. array(1) {
  262. [0]=>
  263. string(10) "Jack Smith"
  264. }
  265. }
  266. }

如您所见,它是一个包含30个元素的数组(一个月中的每一天一个元素)。每个元素都包含一个日期和这一天的预订,所以现在可以很容易地根据需要显示它(例如html)。
如果你想看下个月的节目,可以这样说: file.php 2020-08 您将得到以下输出:

  1. Displaying: 2020-08
  2. 2020-08-01: Jack Smith
  3. 2020-08-02: Jack Smith
  4. 2020-08-03:
  5. 2020-08-04:
  6. 2020-08-05:
  7. 2020-08-06:
  8. 2020-08-07:
  9. 2020-08-08:
  10. 2020-08-09:
  11. 2020-08-10:
  12. 2020-08-11:
  13. 2020-08-12:
  14. 2020-08-13:
  15. 2020-08-14:
  16. 2020-08-15:
  17. 2020-08-16:
  18. 2020-08-17:
  19. 2020-08-18:
  20. 2020-08-19:
  21. 2020-08-20:
  22. 2020-08-21:
  23. 2020-08-22:
  24. 2020-08-23:
  25. 2020-08-24:
  26. 2020-08-25:
  27. 2020-08-26:
  28. 2020-08-27:
  29. 2020-08-28:
  30. 2020-08-29:
  31. 2020-08-30:
  32. Previous month: 2020-07, next month: 2020-09

如果你想要浏览器版本只需通过 YYYY-MM 通过链接参数的参数。如果要从日历中排除出发日期,请修改查询 departureDate > :date

展开查看全部

相关问题