php 日历在 2012 年 2 月之后转到 2013 年 3 月

标签 php mysql

我的酒店房间预订系统有 php mysql calneder。但它显示了一个奇怪的问题我不知道为什么。它不会转到2013年之后,并且在2012年2月之后它会直接转到2013年3月。

另一个问题是我制作日历来显示房间是否已预订。如果我预订 23-25 的房间,它会标记预订 24 和 25,而应该是 23 和 24。以下是日历的代码:

函数.php

     function draw_calendar($month,$year){

/* draw table */
$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

/* table headings */
$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

/* days and weeks vars now ... */
$running_day = date('w',mktime(0,0,0,$month,1,$year));
$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();

/* row for week one */
$calendar.= '<tr class="calendar-row">';

/* print "blank" days until the first of the current week */
for($x = 0; $x < $running_day; $x++):
    $calendar.= '<td class="calendar-day-np">&nbsp;</td>';
    $days_in_this_week++;
endfor;

/* keep going with days.... */
for($list_day = 1; $list_day <= $days_in_month; $list_day++):
    $calendar.= '<td class="calendar-day">';;

        $calendar.= '<div class="day-number">'.$list_day.'</div>';

        /** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !!  IF MATCHES FOUND, PRINT THEM !! **/
        $calendar.= getAllRooms($list_day,$month,$year);

    $calendar.= '</td>';
    if($running_day == 6):
        $calendar.= '</tr>';
        if(($day_counter+1) != $days_in_month):
            $calendar.= '<tr class="calendar-row">';
        endif;
        $running_day = -1;
        $days_in_this_week = 0;
    endif;
    $days_in_this_week++; $running_day++; $day_counter++;
endfor;

/* finish the rest of the days in the week */
if($days_in_this_week < 8):
    for($x = 1; $x <= (8 - $days_in_this_week); $x++):
        $calendar.= '<td class="calendar-day-np">&nbsp;</td>';
    endfor;
endif;

/* final row */
$calendar.= '</tr>';

/* end the table */
$calendar.= '</table>';

/* all done, return result */
return $calendar;
 }
 function getAllRooms($date,$month,$year)
     {
    global $db;
  $where = ' ';
if ($_GET['room_type'] != '') {
    $where .= " HAVING room_type = '".$_GET['room_type']."'";
}

$sql = "SELECT room_type 
        FROM 
            room 
        GROUP BY 
            room_type 
        $where
        ";
/*echo $sql;
exit;*/
$result = $db->Execute($sql);;
$room = '';
while (!$result->EOF) {
    $qs     = '?room_type='.$result->fields('room_type').'&month='.$month.'&  year='.$year;
              $total    =     get_total_rooms_by_type($result->fields('room_type'),$date,$month,$year);
    $room   .=
    '<div class="'.$result->fields('room_type').'"> 

        <a href="'.BASE_URL.'room_detail.php'.$qs.'">
            '.$result->fields('room_type').' ('.$total.')
        </a>
    </div>';
    $result->MoveNext();
}
$result->Close;
return $room;
}
   function get_total_rooms_by_type($room_type,$date,$month,$year)
 {
global $db;
$_newdate   = $year.'-'.$month.'-'.$date;
$sql            = "SELECT room_id FROM room where room_type = '$room_type' ";
$room_results   = $db->Execute($sql);
$room_ids       = array();
while (!$room_results->EOF) {
    $room_ids[] = $room_results->fields('room_id');
    $room_results->MoveNext();
}
$room_results_str = implode(',',$room_ids);
$where = ' where 1 = 1 ';
$available = 1;
if ($_GET['booking_status'] == '1') {
    $where .= ' and (booking_status = 1 or booking_status = 2)';
    $available = 0;
} else if ($_GET['booking_status'] == '2') {
    $where .= ' and (booking_status = 2)';
    $available = 0;
} 
$sql = "select count(room_id) from bookings 
        $where 
            and checkin <= '$_newdate'
            and '$_newdate' <= checkout
            and room_id in ($room_results_str)
        ";
if ($available == 0) {
    return $db->GetOne($sql);

} else {
    return count($room_ids) - $db->GetOne($sql);
}


}
 function draw_calendar_room($month,$year){

/* draw table */
$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

/* table headings */
$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';


/* days and weeks vars now ... */
$running_day = date('w',mktime(0,0,0,$month,1,$year));
$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();

/* row for week one */
$calendar.= '<tr class="calendar-row">';

/* print "blank" days until the first of the current week */
for($x = 0; $x < $running_day; $x++):
    $calendar.= '<td class="calendar-day-np">&nbsp;</td>';
    $days_in_this_week++;
endfor;

/* keep going with days.... */
for($list_day = 1; $list_day <= $days_in_month; $list_day++):
    $calendar.= '<td class="calendar-day">';;

        $calendar.= '<div class="day-number" style=" padding:5px 5px 45px;background-color:'.getRoomColor($list_day,$month,$year).'">'.$list_day.'</div>';

        /** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !!  IF MATCHES FOUND, PRINT THEM !! **/


    $calendar.= '</td>';
    if($running_day == 6):
        $calendar.= '</tr>';
        if(($day_counter+1) != $days_in_month):
            $calendar.= '<tr class="calendar-row">';
        endif;
        $running_day = -1;
        $days_in_this_week = 0;
    endif;
    $days_in_this_week++; $running_day++; $day_counter++;
endfor;

/* finish the rest of the days in the week */
if($days_in_this_week < 8):
    for($x = 1; $x <= (8 - $days_in_this_week); $x++):
        $calendar.= '<td class="calendar-day-np">&nbsp;</td>';
    endfor;
endif;

/* final row */
$calendar.= '</tr>';

/* end the table */
$calendar.= '</table>';

/* all done, return result */
return $calendar;
}
 function getRoomColor($date,$month,$year)
 {
global $db;
$where = ' ';
if ($_GET['room_id'] != '') {
    $room_id    = $_GET['room_id'];

} else {
    $sql = "SELECT room_id
                    FROM 
                        room 
                    WHERE
                        room_type = '".$_GET['room_type']."' order by room_number asc
                    ";
    $room_id = $db->GetOne($sql);;
}
$_newdate = "$year-$month-$date";
$sql = "SELECT booking_status
        FROM 
            bookings 
        where 
            checkin <= '$_newdate'
            and 
            '$_newdate' <= checkout
            and 
            room_id = '$room_id'
        ";
/*echo $sql;
exit;*/
$result = $db->GetOne($sql);;
if ($result == 1) {
    return '#FF0';
} else if ($result == 2) {
    return '#F00';
} else {
    return '#64C733';
}

 }

index.php 用于上一个下一个函数:

     $prev_month = (($month - 1) == 0) ? 12 : $month -1 ;
        $prev_year = ($prev_month == 12) ? $year - 1 : $year;
     ?>
   <h2 class="calender">
     <a href="index.php?month=<?php echo $prev_month ?>&year=<?php echo $prev_year ?>" class="prev"> &gt;&gt;</a>


    <span class="month"><?php echo  date('F',mktime(0,0,0,$month,1,$year)).' '.$year.''; ?></span>
    <?php 
        $next_month = (($month + 1) == 13) ? 1 : $month + 1 ;
        $next_year  = ($prev_month == 1) ? $year + 1 : $year;
     ?>
     <a href="index.php?month=<?php echo $next_month ?>&year=<?php echo $next_year ?>" class="next"> 
        &lt;&lt;</a>

提前感谢大家。

最佳答案

我只看了第二个片段,但我很确定这就是问题所在:

$next_year  = ($prev_month == 1) ? $year + 1 : $year;

如果是二月,$prev_month 设置为 1,因此您的 $next_year 变为 $year + 1

我认为您所要做的就是将该行更改为:

$next_year  = ($month == 12) ? $year + 1 : $year;

关于php 日历在 2012 年 2 月之后转到 2013 年 3 月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8945315/

相关文章:

php - Magento更新每商店价格查看SQL

php - 如何一个接一个地执行多个查询?

php - 检查是否存在以给定字符串开头的文件

php - 使用 PHP 将两个/多个图像上传到 MySQL

mysql - 识别 MySQL MERGE 存储引擎中的成员表

php - 过去 24 小时内查看最多的主题

mysql - 具有不同字段名称的 SQL 多选查询?

java - 将 Java hashmap 初始值设定项转换为 PHP 等效项

php - Symfony 2.4 你请求了一个不存在的服务 "payment.encryption_service"

c# - Mysql 查询在作为文本传递到数据库时抛出错误,但作为存储过程工作