Echo all months with days calendar

后端 未结 5 1758
星月不相逢
星月不相逢 2021-02-06 20:12

I am using the code below to echo the current month. How can i enhance it so that is shows all the months with the names and days and dates..

Code:

 &         


        
5条回答
  •  别跟我提以往
    2021-02-06 20:14

    Try this:

    function getDates($year)
    {
        $dates = array();
    
        date("L", mktime(0,0,0, 7,7, $year)) ? $days = 366 : $days = 365;
        for($i = 1; $i <= $days; $i++){
            $month = date('m', mktime(0,0,0,1,$i,$year));
            $wk = date('W', mktime(0,0,0,1,$i,$year));
            $wkDay = date('D', mktime(0,0,0,1,$i,$year));
            $day = date('d', mktime(0,0,0,1,$i,$year));
    
            $dates[$month][$wk][$wkDay] = $day;
        } 
    
        return $dates;   
    }
    

    it will return an array of months->weeks->day->weekday of the year you pass to the function. Hopefully it should be easy to traverse through the array to print everything out. Am sure there are a lot of tweaks you can make to that but its a start.

    I would also try and stay away from printing out html using echo, for example instead of;

    echo '';
    for($i=1;$i<$firstDay['wday'];$i++){
        echo ' ';
    }
    

    do;

    ;
    
        
    
    

    It kind of makes the code more readable I think.

    EDIT: Just thought I should include an example of a use case as well, as below:

    
     $weeks) { ?>
     $days){ ?>
        
    ', $weekdays); ?>

    Which gives you the output:

    enter image description here

提交回复
热议问题