获取两个日期范围之间的所有日期,并根据所选周

时间:2016-07-12 09:19:58

标签: php date

我需要在2016-10-01至2016-11-01之间设置两个日期,例如范围日期,并生成所有星期六和星期日日期范围。

例如这个回报:

2016-10-1

2016-10-2

2016-10-8

2016-10-9

2016-10-15

2016-10-16

2016-10-22

2016-10-23

2016-10-29

2016-10-30

注意:不仅是周末。例如我有和像这样的数组:

array('monday','saturday','sunday')

我尝试使用此代码,但这不能正常运行:

$event_da = '2016-10-01';
$event_a = '2016-11-01';

$weekdays = array('saturday','sunday');

for ( $i = $event_da; $i <= $event_a; $i = $i + 86400 ) {

    $thisDate = date( 'Y-m-d', $i );

    $getDate = date('l', strtotime($thisDate));

    if ( in_array( $getDate, $weekdays) ) {

          echo $thisDate;

    }
}

6 个答案:

答案 0 :(得分:1)

$start = strtotime( '2016-05-01 00:00' );
$end = strtotime( '2016-07-10 00:00' );

// Loop between timestamps, 24 hours at a time
for ( $i = $start; $i <= $end; $i = $i + 86400 ) {

$thisDate = date( 'Y-m-d', $i )
$getDate = date('l', strtotime($thisDate));
if ($getDate = 'Saturday' OR $getDate = 'Sunday') {
}
  echo $thisDate 
}
}

答案 1 :(得分:0)

function createDateRangeArray($strDateFrom,$strDateTo)
{
    // takes two dates formatted as YYYY-MM-DD and creates an
    // inclusive array of the dates between the from and to dates.

    // could test validity of dates here but I'm already doing
    // that in the main script

    $aryRange=array();

    $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
    $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

    if ($iDateTo>=$iDateFrom)
    {
        array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
        while ($iDateFrom<$iDateTo)
        {
            $iDateFrom+=86400; // add 24 hours
            array_push($aryRange,date('Y-m-d',$iDateFrom));
        }
    }
    return $aryRange;
}

来源:https://stackoverflow.com/a/4312491/1790315

答案 2 :(得分:0)

如果你有PHP&gt; = 5.1:

 while (strtotime($date) <= strtotime($end_date)) 
    {
        function isWeekend($date) {
            return (date('N', strtotime($date)) >= 6);
           $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
          }

    }
?>

否则:

while (strtotime($date) <= strtotime($end_date)) 
{
    function isWeekend($date) {
        $weekDay = date('w', strtotime($date));
        $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
        return ($weekDay == 0 || $weekDay == 6);
     }
}

答案 3 :(得分:0)

你走了。这是一个非常灵活的代码,您可以在其中输入任意两个日期,另一个数组指定要过滤的日期。

function isWeekend($start_date, $end_date, $expected_days) {
    $start_timestamp = strtotime($start_date);
    $end_timestamp   = strtotime($end_date);
    $dates = array();
    while ($start_timestamp <= $end_timestamp) {
        if (in_array(date('l', $start_timestamp), $expected_days)) {
           $dates[] = date('Y-m-d', $start_timestamp);
        }
        $start_timestamp = strtotime('+1 day', $start_timestamp);
    }
    return $dates;
  }

 $start_date        =  '2016-10-01';
 $end_date          =  '2016-11-01';
 $expected_days     =  array('Monday','Saturday','Sunday');
 $weekend_dates     =  isWeekend($start_date, $end_date, $expected_days);

输出:

 Array
(
  [0] => 2016-10-01
  [1] => 2016-10-02
  [2] => 2016-10-03
  [3] => 2016-10-08
  [4] => 2016-10-09
  [5] => 2016-10-10
  [6] => 2016-10-15
  [7] => 2016-10-16
  [8] => 2016-10-17
  [9] => 2016-10-22
  [10] => 2016-10-23
  [11] => 2016-10-24
  [12] => 2016-10-29
  [13] => 2016-10-30
  [14] => 2016-10-31
)

答案 4 :(得分:0)

试试这个:

$days = array('Monday', 'Saturday', 'Sunday');

for($i = strtotime('2016-10-01'); $i <= strtotime('2016-11-01'); $i = strtotime('+1 day', $i)) {
  if (in_array(date('l', $i), $days)) {
    echo date('l Y-m-d', $i);
  }
}

答案 5 :(得分:0)

以下代码只是Jan Walczak的编辑版

$start = strtotime( '2016-05-01 00:00' );
$end = strtotime( '2016-07-10 00:00' );
for ( $i = $start; $i <= $end; $i = $i + 86400 ) {
    $thisDate = date( 'Y-m-d', $i );
    $getDate = date('l', strtotime($thisDate));
    //echo $getDate . "<br>";
    if ($getDate == 'Saturday' || $getDate == 'Sunday') 
    {
      echo $thisDate . "<br>";
    }
}