gpt4 book ai didi

php - 获取没有星期五的月份中的第几天

转载 作者:搜寻专家 更新时间:2023-10-31 21:41:12 25 4
gpt4 key购买 nike

我需要获取一个月中的天数,但不包括星期五。

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

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

最佳答案

我找到了一个方便的函数 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('', strtotime("$month 2012")) - num_days('Friday', $month, 2012);

前提是 $month 是月份的全称(不是数字)。

关于php - 获取没有星期五的月份中的第几天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10886519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com