2015-06-15 161 views
0

我有我創建生成一個日曆下面的代碼,但仍存在一些問題:PHP日曆問題

//Labels 
$dayLabels   = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"); 
$monthLables  = array("January","February","March","April","May","June","July","August","September","October","November","December"); 

//max values 
$maxDays    = 7; 
$maxMonths   = 12; 

//stats 
$forceMonth    = $_GET['m']; 
$forceYear   = $_GET['y']; 

$todayDate   = date("d-m-Y"); 
$todayDate   = date("d-m-Y", strtotime($todayDate)); 
$explodeToday  = explode("-", $todayDate); 

$currentDay   = $explodeToday[0]; 

if(isset($forceMonth)) { 
    $currentMonth = $forceMonth; 
} else { 
    $currentMonth = $explodeToday[1]; 
}; 

if(isset($forceYear)) { 
    $currentYear  = $forceYear; 
} else { 
    $currentYear  = $explodeToday[2]; 
}; 

$daysInMonth  = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $currentYear); 

//database values 
$startDate   = array("01-06-2015","25-06-2015"); 
$endDate    = array("05-06-2015","05-07-2015"); 
$bookedUser   = array("Dexter","James"); 

//counters 
$daysIntoMonth  = 0; 
$dayCounter   = 0; 

//debug 
echo '<p>Current Month: ' .$monthLables[$currentMonth-1]. '/' .$currentMonth. '</p>'; 
echo '<p>Current Year: ' .$currentYear. '</p>'; 

//start of Calendar 
echo '<table>'; 

//print days of week 
echo '<tr>'; 
foreach($dayLabels as $day) { 
    echo '<td style="border-bottom:dashed 1px #DDD;">' .$day. '</td>'; 
}; 
echo '</tr>'; 
while($daysIntoMonth < $daysInMonth) { 
    //days into month 
    $daysIntoMonth++; 
    $temp_inMonth   = sprintf("%02d", $daysIntoMonth); 
    $daysIntoMonth  = $temp_inMonth; 

    //days into week 
    $dayCounter++; 
    $temp_dayCounter = sprintf("%02d", $dayCounter); 
    $dayCounter    = $temp_dayCounter; 

    //current calendar date 
    $calDate     = date('d-m-Y', strtotime($daysIntoMonth. '-' .$currentMonth. '-' .$currentYear)); 
    $timeCal     = strtotime($calDate); 

    if($dayCounter == 1) { 
     echo '<tr>'; 
    }; 

    if($startKey = array_search($calDate, $startDate) !== FALSE) { 
     $booked = true; 
    }; 

    if($endKey = array_search($calDate, $endDate) !== FALSE) { 
     $booked = false; 
    }; 

    if($booked == true) { 
     echo '<td style="background-color:red;">' .$calDate. '/' .$daysIntoMonth. ' ' .$dayCounter. '</td>'; 
    } else if($booked == true && array_search($calDate, $startDate) !== FALSE) { 
     echo '<td style="background-color:red;">' .$calDate. '/' .$daysIntoMonth. ' ' .$dayCounter. '</td>'; 
    } else if($booked == false && array_search($calDate, $endDate) !== FALSE) { 
     echo '<td style="background-color:red;">' .$calDate. '/' .$daysIntoMonth. ' ' .$dayCounter. '</td>'; 
    } else { 
     echo '<td>' .$calDate. '/' .$daysIntoMonth. ' ' .$dayCounter. '</td>'; 
    } 

    if($dayCounter == $maxDays) { 
     echo '</tr>'; 
     $dayCounter = 0; 
    }; 
}; 

//table is kill 
echo '</table>'; 

我已經注意到了這些問題:

  1. 無法把$bookedUser爲分別爲$startDate,$endDate
  2. 當預訂跳到另一個月時,它會跳過所有日期,直到$endDate
  3. 所有月份開始於Monday,我該如何去讓他們開始正確的一週的幾天。

可能的代碼示例,以幫助我解決我的問題將是偉大的,在此先感謝。

編輯:

我已經通過使用下面的代碼解決的問題3:

$firstDayofMonth = strtotime("01-$currentMonth-$currentYear"); 
$firstDayofMonth = date("D", $firstDayofMonth); 
$firstDayofMonth = array_search($firstDayofMonth, $dayMiniLabels); 
$firstDayofMonth = $firstDayofMonth + 1; 

$startMonth   = 0; 

if($firstDayofMonth != 7) { 
    while($startMonth < $firstDayofMonth) { 
     echo '<td></td>'; 
     $startMonth++; 
     $dayCounter++; 
     $temp_dayCounter = sprintf("%02d", $dayCounter); 
     $dayCounter    = $temp_dayCounter; 

    }; 
}; 
+0

_Still lookings for Answers_ – 099

回答

0

爲日,月(問題3),我這樣做:

$todaysNumber = date('w'); 
$currentDayInText = $dayLabels[$todaysNumber]; 

和monhts一樣。

大部分情況下,在我的MySQL表中,日期的位置與2015-06-05相似,而不是歐洲時間標記。也許這可以解決問題1

+0

嗨,感謝您的回覆,我剛剛修復了問題3,就像您發佈此答案一樣,請隨時查看我的修復程序。問題1正在被'array_search'調用,由於某種原因,它沒有給出正確的數組鍵,然後我可以使用它從'$ bookedUser'中找到正確的信息 – 099