如何计算任何月份的周数和星期几

Lay*_*ric 1 php algorithm

如何计算任何月份的周数和星期几.

Mār*_*dis 12

好的,我想我找到了你:

$days = cal_days_in_month(CAL_GREGORIAN, 1, 2011);

$week_day = date("N", mktime(0,0,0,1,1,2011));

$weeks = ceil(($days + $week_day) / 7);

echo $weeks;
Run Code Online (Sandbox Code Playgroud)


sgo*_*les 5

此代码返回一个月的天数.

<?php
for ($year = 2011; $year <= 2012; $year++){
    for ($month = 1; $month <= 12; $month++){
        $num_of_days = date("t", mktime(0,0,0,$month,1,$year));
        $month_Year = date("F",mktime(0, 0, 0, $month, 1, $year));
        echo "<b>$month_Year, $year </b><BR>";
        echo "Number of days = $num_of_days <BR>";
        $firstdayname = date("D", mktime(0, 0, 0, $month, 1, $year));
        $firstday = date("w", mktime(0, 0, 0, $month, 1, $year));
        $lastday = date("t", mktime(0, 0, 0, $month, 1, $year));
        $lastdayname = date("D", mktime(0, 0, 0, $month, $lastday, $year));
        echo "First day of the month = $firstday,$firstdayname <BR> ";
        echo "Last day of the month = $lastday,$lastdayname <BR> ";
        $no_of_weeks = 1;
        $count_weeks = 0;
        while($no_of_weeks <= ($lastday+$firstday)){
            $no_of_weeks += 7;
            $count_weeks++;
        }
        echo "Number Of weeks = $count_weeks <br><br>";
    }
}
?>
Run Code Online (Sandbox Code Playgroud)