user3189194
user3189194

Reputation: 3

how can i exclude saturday and sunday when i generate dates

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];
}}

Upvotes: 1

Views: 2520

Answers (3)

Dzmitry Urbanovich
Dzmitry Urbanovich

Reputation: 1

I used:

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

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

Upvotes: 0

mkabanen
mkabanen

Reputation: 615

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];
}}

Upvotes: 0

chen
chen

Reputation: 407

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.

Upvotes: 1

Related Questions