0

I need to add days in the given date but i need to exclude Sunday.

Here is my code which is working fine but i am not able to exclude the Sunday.

    $service_start_date = new DateTime('2020-10-18');
    $service_end_date = new DateTime('2020-10-25');
//echo $service_start_date;
   $daterange = new DatePeriod($service_start_date, new DateInterval('P1D'), $service_end_date);
       foreach($daterange as $date){
         $service_date = $date->format("Y-m-d");
         echo $service_date;
    }
Roxx
  • 3,738
  • 20
  • 92
  • 155

1 Answers1

0

Use the DatePeriod class. There's a related question explaining how to do the same, except he/she is excluding the entire weekend: Get date range between two dates excluding weekends

You can also loop through you're dates and check for Sundays to exclude them, like suggested in the comments (to make this answer complete).

Tom
  • 387
  • 2
  • 12
  • 1
    Maybe this should just have been a close vote with that linked as a duplicate. As you point out, the accepted answer with a single number change will do what OP wants. – Jonnix Oct 18 '20 at 13:07
  • @Jonnix I agree, but I don't think I can mark posts as duplicate with my reputation so I had to do it this way for as far as I know. :) – Tom Oct 18 '20 at 13:10
  • I can't remember what rep you need to do that, but reason seems fair. – Jonnix Oct 18 '20 at 13:11
  • 1
    To flag a duplicate, just leave a comment and someone else may do the closing for you. – Nigel Ren Oct 18 '20 at 13:41