-5

I want all dates from date within a date range,but i also want to exclude weekends within the range like Friday and Saturday.But i want weekend will be dynamic means it can be any other day.

for($date = $fromdate; $date->lte($todate); $date->addDay())
             {
                foreach($allweekends as $weekend)
                {
                    if($date->format('l')!=$weekend->weekendDay )
                    {
                        $daterange[]=[

                            'date'=>$date->format('Y-m-d'),
                            'day'=>$date->format('l'),
                        ];
                    }
                }    
            } 
Include13
  • 3
  • 4
  • Please go read [ask] and [mcve], and edit your question accordingly. – CBroe Jun 28 '18 at 10:56
  • Possible duplicate of [I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?](https://stackoverflow.com/questions/3207749/i-have-2-dates-in-php-how-can-i-run-a-foreach-loop-to-go-through-all-of-those-d) – Aziz Saleh Jun 28 '18 at 11:19

1 Answers1

-1

This should do what you need:

<?php

$begin = new DateTime('2018-06-28');
$end = new DateTime('2018-07-02');

for($date = $begin; $date <= $end; $date->modify('+1 day')){
    if (date('N', strtotime($date->format('Y-m-d'))) < 6) {
        $daterange[]=[
            'date'=>$date->format('Y-m-d'),
            'day'=>$date->format('l'),
        ];
    }
}
var_dump($daterange);

?>

It's adapted from this answer https://stackoverflow.com/a/3207849/4028570.

Nuno Sousa
  • 7
  • 1
  • 2
  • weekend will be dynamic and it will come from database. – Include13 Jun 28 '18 at 11:26
  • Then you just need to set the $begin and $end variables according to your database query. Also you can just put that code inside a function with $begin and $end as parameters and call it whenever you need it. – Nuno Sousa Jun 28 '18 at 11:30