我正在為一個小型預訂系統設計一個小型 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 關注
- 116 瀏覽
添加回答
舉報
0/150
提交
取消