-1

i need to put two dates, for example a range dates from 2016-10-01 to 2016-11-01 and generate all saturday and sunday dates in that range.

For example this return:

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

NOTE: Not only weekends. For example i have and array like this:

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

I'm try this code, but this not work correctly:

$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;

    }
}
miniMAC
  • 35
  • 1
  • 5

6 Answers6

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 
}
}
Whencesoever
  • 2,218
  • 15
  • 26
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;
}

Source: https://stackoverflow.com/a/4312491/1790315

Community
  • 1
  • 1
awl19
  • 366
  • 4
  • 10
  • Don't copy an entire answer from another SO answer. If you think it is a duplicate flag/vtc as duplicate. – Rizier123 Jul 12 '16 at 09:29
  • That answer was copied from another answer. And so the cycle continues. But I get you about the duplicates, will follow in the future. – awl19 Jul 12 '16 at 09:29
0

If you have PHP >= 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)));
          }

    }
?>

otherwise:

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);
     }
}
Harshad Hirapara
  • 462
  • 3
  • 12
0

Here you go. This is a pretty flexible code where you can enter any two dates and another array specifying the days you want to filter.

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);

Output:

 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
)
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
  • Thanks @Object Manipulator your function work and finally the code is:`function rangeDate($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(strtolower(date('l', $start_timestamp)), $expected_days)) { $dates[] = date('Y-m-d', $start_timestamp); } $start_timestamp = strtotime('+1 day', $start_timestamp); } return $dates; }`I've added **strtolower** to ensure validation between arrays – miniMAC Jul 12 '16 at 10:34
0

Try this one:

$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);
  }
}
Ihor
  • 277
  • 4
  • 16
0

Following Code is just Edit version of 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>";
    }
}
Hasnat
  • 151
  • 1
  • 5