1

I'm generating dates using my code I want to exclude sunday and saturday please check my code here

for ($date = $start_date; $date <= $end_date; $date = date('Y-m-d', strtotime($date . ' + 1 day'))) {

$week = date('W', strtotime($date));
$year = date('Y', strtotime($date));
$from = date("Y-m-d", strtotime("$date")); 
if ($from < $start_date)
    $from = $start_date;
$to = date("Y-m-d", strtotime("$date-1day + 1 week"));   
if ($to > $end_date) {
    $to = $end_date;
}
if ($from <= $to) {
    array_push($weekfrom, $from);
    array_push($weekto, $to);
}

$n = count($weekfrom);

for ($i = 0; $i < $n; $i++) {
    echo $weekfrom[$i];
}}
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
  • there's a missing closing bracket – Sebas Jan 13 '14 at 06:46
  • oops sorry i forgot :) edited already :) thanks – user3189194 Jan 13 '14 at 06:49
  • i don't understand this line: `$to = date("Y-m-d", strtotime("$date-1day + 1 week")); //Returns the date of sunday in week`. To me, you just add 6 days, how does it become a sunday? – Sebas Jan 13 '14 at 06:50
  • sorry for the comment right there :) wrong comment in my code – user3189194 Jan 13 '14 at 07:00
  • I'd use the `DateTime` and `DatePeriod` objects like in [this answer](https://stackoverflow.com/a/3352805/1948292) This question is also a duplicate of [this question](https://stackoverflow.com/questions/3352712/get-date-range-between-two-dates-excluding-weekends). – Daniel W. Sep 15 '21 at 10:06

3 Answers3

1

you can do like this.

$getDate = date('l', strtotime($date));
if ($getDate != 'Saturday' AND $getDate != 'Sunday') {
    ......
}

if that date not Saturday or Sunday, then process the thing.

chen
  • 407
  • 2
  • 9
0

Just add this to beginning of your loop:

if(date("w", strtotime($date)) == 0 || date("w", strtotime($date)) == 6) continue;

Like this:

for ($date = $start_date; $date <= $end_date; $date = date('Y-m-d', strtotime($date . ' + 1 day'))) {

if(date("w", strtotime($date)) == 0 || date("w", strtotime($date)) == 6) continue;

$week = date('W', strtotime($date));
$year = date('Y', strtotime($date));
$from = date("Y-m-d", strtotime("$date")); //Returns the date of monday in week
if ($from < $start_date)
$from = $start_date;
$to = date("Y-m-d", strtotime("$date-1day + 1 week")); //Returns the date of sunday in week
if ($to > $end_date) {
$to = $end_date;
}
if ($from <= $to) {
array_push($weekfrom, $from);
array_push($weekto, $to);
}

$n = count($weekfrom);

for ($i = 0; $i < $n; $i++) {
echo $weekfrom[$i];
}}
mkabanen
  • 615
  • 5
  • 15
  • works like magic :)... but can i ask last question im planing to have a separate code like this for sunday and saturday i mean i have a check box to exclude saturday and exclude sunday – user3189194 Jan 13 '14 at 07:04
  • if(sunday_box_checked && date("w", strtotime($date)) == 0) continue; and if(saturday_box_checked && date("w", strtotime($date)) == 6) continue; – mkabanen Jan 13 '14 at 07:07
  • Add these 2 if() statements to beginning of your loop and it works. Sunday_box_checked and saturday_box_checked must be boolean values you get, if you have checked checkboxes. – mkabanen Jan 13 '14 at 07:09
  • i already din put 6 for saturday and 7 sunday but in checkbox for saturday it doesnt show the sunday – user3189194 Jan 13 '14 at 07:31
0

I used:

strtotime(sprintf('+%d weekday', 3));

if now is 15 sep(Wednesday), the example returns: 20 sep(exclude weekends)