得到没有星期五的月份的数字

时间:2012-06-04 18:44:59

标签: php date

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

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

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

1 个答案:

答案 0 :(得分:1)

我找到了一个方便的功能here

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是一个月的全名(不是数字)。