php function for get all mondays within date range

Nisha picture Nisha · Aug 15, 2011 · Viewed 28.3k times · Source

Example: $startDate is Monday 2007-02-05 and $endDate is Tuesday 2007-02-20. Then I want it to list:

Monday 2007-02-05
Monday 2007-02-12
Monday 2007-02-19

I looked at the PHP manual and found this to get all the days between two dates. But how to do it the way i want? PHP Code:

Answer

Paul picture Paul · Aug 15, 2011

Rather than get all days and loop through them all, get the first Monday after the start date and then iterate 7 days at a time:

$endDate = strtotime($endDate);
for($i = strtotime('Monday', strtotime($startDate)); $i <= $endDate; $i = strtotime('+1 week', $i))
    echo date('l Y-m-d', $i);