重复的天数计入1月份

时间:2017-01-02 08:14:38

标签: php

2016年的php日历显示正确的日子,当我将其更改为2017年时,它显示了1月份的重复日期。现在我有2016年和2017年硬编码,之后我会将此更改为系统日期。有人请帮助我吗?

<?php 
    $dates = getDates(date("Y")); //gets system default date, if i change this to date(2016) works fine, when i change to 2017 it shows repeated days entry for january month.

    $weekdays = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'); //prints weeks.
    $monthPrints = array('01'=>'January','02'=>'February','03'=>'March','04'=>'April','05'=>'May','06'=>'June','07'=>'July','08'=>'August','09'=>'September','10'=>'October','11'=>'November','12'=>'December'); ?>  // prints month string.

<?php foreach($dates as $month => $weeks) { ?>
    <?php print_r($monthPrints[$month]); ?>
    <table id="<?= $month ?>">    
        <tr>
            <th><?php echo implode('</th><th>', $weekdays); ?></th>
        </tr>
        <?php foreach($weeks as $week => $days){ ?>
        <tr>
         <?php foreach($weekdays as $day){ ?>
            <td>
                <?php echo isset($days[$day]) ? $days[$day] : '&nbsp'; ?>
            </td>               
            <?php } ?>
        </tr>
        <?php } ?>
    </table>
    <?php  }?>
    <?php
    function getDates($year)
    {
        $dates = array();

        for($i = 1; $i <= 366; $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;
    }
?>

enter image description here

1 个答案:

答案 0 :(得分:2)

问题是for中的getDates()周期达到366.将此数字更改为365,一切都会好的。 我想这是因为2016年是bissextile,而2017年不是。

您应该使用不同的东西来获取一年中的日期,请查看以下日期:Php how to go from day of the year to date and vice versa