PHP日历显示年份

时间:2018-04-09 14:17:37

标签: php

我有一些日历代码。它可以正常工作和显示。但是,我想知道有没有办法展示多年? 目前它只是显示当前年份(2018年),但我希望它的旁边有按钮可以到明年,2019年等。

见下面的代码。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

这个怎么样:

在我的示例中,您需要将另一个参数传递给查询:y 有了这个,您将能够创建多年的链接: /page.php?y=1999&m=4

$monthArg = !empty($_GET['m']) ? intval($_GET['m']) : date('m');
$yearArg  = !empty($_GET['y']) ? intval($_GET['y']) : date('Y'); // you will need to add an other query parameter for this

$currYear  = $yearArg;
$startDate = strtotime($currYear . "-" . $monthArg . "-01 00:00:01");
$startDay  = date("N", $startDate);
$monthName = date("M",$startDate );

$daysInMonth = cal_days_in_month(CAL_GREGORIAN, date("m", $startDate), date( "Y", $startDate));
$endDate     = strtotime($currYear . "-" . $monthArg . "-" .  $daysInMonth ." 00:00:01");

$endDay = date("N", $endDate);

if ($startDay> 6)
    $startDay = 7 - $startDay;

$currElem        = 0;
$dayCounter      = 0;
$firstDayHasCome = false;
$arrCal          = [];

for($i = 0; $i <= 5; $i ++) {
    for($j= 0; $j <= 6; $j++) {
                // decide what to show in the cell
        if($currElem < $startDay && !$firstDayHasCome)          
            $arrCal[$i][$j] = "";
        else if ($currElem == $startDay && !$firstDayHasCome) {
            $firstDayHasCome= true;
            $arrCal[$i][$j] = ++$dayCounter;
        }
        else if ($firstDayHasCome) {
            if ($dayCounter < $daysInMonth)
                $arrCal[$i][$j] = ++ $dayCounter; 
            else
                $arrCal[$i][$j] = "";   
        }                           

        $currElem ++;               
    }
}