1か月の日数を取得する必要がありますが、たとえば金曜日は取得しません。
$num = cal_days_in_month(CAL_GREGORIAN, $month, 2012);
ここで合計日数を取得できますが、カウントから金曜日を差し引く必要があります。どうすればいいのですか?
ここで便利な関数を見つけました:
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
月のフルネーム(数字ではありません)が提供されます。