2015-06-15 118 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