从日期范围中提取日期

时间:2016-09-10 20:35:18

标签: php if-statement foreach

我的部分回答是动态回答的: How to extract every Monday and every fortnightly Monday from a range of two dates in PHP?

我添加了一个foreach循环来打印这个例子中的所有星期一:

$start = strtotime('2016-09-05');
$end = strtotime('2016-09-30');

$mondays=array(); 
$tuesdays=array();  

while( $start <= $end  ) {

if ( date('N',$start)==1 )   
$mondays[]=$start;
$start += 86400;  

}  
foreach ($mondays as $item) {  
print date("D. Y-m-d", ($item)); 
print ("<br>");
}

我得到了这些结果: enter image description here

但是我如何添加else / if语句来显示星期二,例如

如果日期以$ start = strtotime('2016-09-06');

开头

我希望它显示: enter image description here

感谢。

2 个答案:

答案 0 :(得分:0)

与您当前的方法保持一致:

<form method="post" action="">
Search Member:
<input type="text" name="member">
<input type="submit" value="Filter">
</form>

答案 1 :(得分:0)

在此代码中,我们首先找到DayOfWeek,然后找到范围内的所有自由度:     

// Data

$StartDate = strtotime('2016-09-06');
$EndDate   = strtotime('2016-09-30');

$DayOfWeek = date("l", $StartDate); // Determine day of week

$LoopDate = $StartDate;

// Find all DayOfWeek's Between Start and End

$Result = [$StartDate];

while (true) {
    $NextDayOfWeek = strtotime("next " . $DayOfWeek, $LoopDate);

    if ($NextDayOfWeek > $EndDate) {
        break;
    }

    $Result[] = $LoopDate = $NextDayOfWeek;
}

// Print Result

/**
 | 2016-09-06
 | 2016-09-13
 | 2016-09-20
 | 2016-09-27
 */
foreach( $Result as $Row ) {
    echo date("Y-m-d", $Row) . '<br/>';
}