且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

查找两个日期之间的特定日期的PHP PHP不使用任何循环?

更新时间:2023-01-17 13:19:32

function countDays( $ day,$ start,$ end)
{

function countDays($day, $start, $end) {

$start = strtotime($start);
$end = strtotime($end);
$datediff = $end - $start;

$days =  floor($datediff / (60 * 60 * 24));


//get the day of the week for start and end dates (0-6)
$w = array( date('w', $start ), date('w', $end) );

//get partial week day count
if ($w[0] < $w[1])
{            
    $partialWeekCount = ($day >= $w[0] && $day <= $w[1]);
}else if ($w[0] == $w[1])
{
    $partialWeekCount = $w[0] == $day;
}else
{
    $partialWeekCount = ($day >= $w[0] 
    $day <= $w[1]); 
} 
//first count the number of complete weeks, then add 1 if $day falls in a partial week. 
return intval($days / 7) + $partialWeekCount; 

}

函数调用-countDays( 5, 2017-04-14, 2017-04-21)

Function Call - countDays( 5, "2017-04-14", "2017-04-21")