3

我需要得到一个月的天数,但没有例如星期五。

$num = cal_days_in_month(CAL_GREGORIAN, $month, 2012);

在这里我可以得到总天数,但我需要从计数中减去星期五。我该怎么做?

4

1 回答 1

0

我在这里找到了一个方便的功能:

function num_days ($day, $month, $year) { 
    $day_array = array("Mon" => "Monday", "Tue" => "Tuesday", "Wed" => "Wednesday", "Thu" => "Thursday", "Fri" => "Friday", "Sat" => "Saturday", "Sun" => "Sunday");

    $month_array = array(1 => "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

    /* * Check our arguments are valid. */

    /* * $day must be either a full day string or the 3 letter abbreviation. */ 
    if (!(in_array($day, $day_array) || array_key_exists($day, $day_array))) { 
        return 0; 
    }

    /* * $month must be either a full month name or its 3 letter abrreviation */ 
    if (($mth = array_search(substr($month,0,3), $month_array)) <= 0) { 
        return 0; 
    }

    /* * Now fetch the previous $day of $month+1 in $year; * this will give us the last $day of $month. */

    /* * Calculate the timestamp of the 01/$mth+1/$year. */ 

    $time = mktime(0,0,0,$mth+1,1,$year);
    $str = strtotime("last $day", $time);

    /* * Return nth day of month. */ 

    $date = date("j", $str);

    /* * If the difference between $date1 and $date2 is 28 then * there are 5 occurences of $day in $month/$year, otherwise * there are just 4. */ 

    if ($date <= 28) { 
        return 4; 
    } else { 
        return 5; 
    } 
} 

用法:

echo date('t', strtotime("$month 2012")) - num_days('Friday', $month, 2012);

提供$month的是月份的全名(不是数字)。

于 2012-06-04T19:51:33.853 回答