我正在为一个小型预订系统设计一个小型 PHP 日历。最后一个功能是阻止一些不可用的日期,我已将其存储在 PHP 数组中。实现这一目标的最佳方法是什么?请我需要一些关于如何做到这一点的建议。谢谢// Set your timezone!!date_default_timezone_set('Europe/Madrid');// Get prev & next monthif (isset($_GET['ym'])) { $ym = $_GET['ym'];} else { // This month $ym = date('Y-m');}// Check format$timestamp = strtotime($ym . '-01'); // the first day of the monthif ($timestamp === false) { $ym = date('Y-m'); $timestamp = strtotime($ym . '-01');}// Today (Format:2018-08-8)$today = date('Y-m-j');// Title (Format:August, 2018)$title = date('F, Y', $timestamp);// Create prev & next month link$prev = date('Y-m', strtotime('-1 month', $timestamp));$next = date('Y-m', strtotime('+1 month', $timestamp));// Number of days in the month$day_count = date('t', $timestamp);// 1:Mon 2:Tue 3: Wed ... 7:Sun$str = date('N', $timestamp);// Array for calendar$weeks = [];$week = '';// Add empty cell(s)$week .= str_repeat('<td></td>', $str - 1);for ($day = 1; $day <= $day_count; $day++, $str++) { $date = $ym . '-' . $day; if ($today == $date) { $week .= '<td class="today">'; } else { $week .= '<td>'; } $week .= $day . '</td>'; // Sunday OR last day of the month if ($str % 7 == 0 || $day == $day_count) { // last day of the month if ($day == $day_count && $str % 7 != 0) { // Add empty cell(s) $week .= str_repeat('<td></td>', 7 - $str % 7); } $weeks[] = '<tr>' . $week . '</tr>'; $week = ''; }}如果我在 php 中有一个数组,其日期格式为“2020-08-12”,该数组必须不可用或不可选择,我该怎么办。
1 回答

BIG阳
TA贡献1859条经验 获得超6个赞
对于<td>您输出的每个内容,检查其日期是否在不可用日期列表中,并输出一个类名称,以便您可以使用 CSS 定位它。
更改此代码:
if ($today == $date) {
$week .= '<td class="today">';
} else {
$week .= '<td>';
}
$week .= $day . '</td>';
对此:
$classes = []; // assume
if ( in_array( $date, $unavailable_dates ) ) {
$classes[] = 'unavailable';
}
if ($today == $date) {
$classes[] = 'today';
}
$week .= '<td class="' . join(' ', $classes ) . '"></td>';
这假设不可用日期的数组存储在$unavailable_dates,例如['2020-08-10', '2020-08-12', '2020-08-17']。
- 1 回答
- 0 关注
- 92 浏览
添加回答
举报
0/150
提交
取消