我正在為一個(gè)小型預(yù)訂系統(tǒng)設(shè)計(jì)一個(gè)小型 PHP 日歷。最后一個(gè)功能是阻止一些不可用的日期,我已將其存儲在 PHP 數(shù)組中。實(shí)現(xiàn)這一目標(biāo)的最佳方法是什么?請我需要一些關(guān)于如何做到這一點(diǎn)的建議。謝謝// 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 中有一個(gè)數(shù)組,其日期格式為“2020-08-12”,該數(shù)組必須不可用或不可選擇,我該怎么辦。
1 回答

BIG陽
TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
對于<td>您輸出的每個(gè)內(nèi)容,檢查其日期是否在不可用日期列表中,并輸出一個(gè)類名稱,以便您可以使用 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>';
這假設(shè)不可用日期的數(shù)組存儲在$unavailable_dates,例如['2020-08-10', '2020-08-12', '2020-08-17']。
- 1 回答
- 0 關(guān)注
- 126 瀏覽
添加回答
舉報(bào)
0/150
提交
取消