PHP日历问题

时间:2015-06-15 09:04:11

标签: php calendar

我创建了以下用于生成日历的代码,但它有一些问题:

//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开始,我将如何让他们开始正确的一周。
  4. 可以提前帮助我解决问题的代码示例很棒。

    编辑:

    我使用以下代码解决了问题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;
    
        };
    };
    

1 个答案:

答案 0 :(得分:0)

对于日期和月份(问题3 ),我会这样做:

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

对于monhts也一样。

大多数情况下,在我的MySQL表格中,日期的位置与2015-06-05相同,而不是欧洲时间表。也许这可以解决问题1