1

I want to find occurrence of dates of a day between a date range.

E.g. date1 = 04/12/2012 (Thursday) and date2 = 04/30/2012.

So PHP function should return two dates:

19/12/2012 (1st Thursday after start date) and 26/12/2012 (2nd Thursday after start date).

For large date range function should return 1st,2nd,3rd,4th....so on occurrence dates.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
naresh
  • 33
  • 7
  • Hi Josh. Thanks for your reply. I have tried to find all the intermediate dates between two dates but don't have idea about find particular dates of day. – naresh Apr 12 '12 at 15:04
  • possible duplicate of [Getting all dates for Mondays and Tuesdays for the next year](http://stackoverflow.com/questions/2045736/getting-all-dates-for-mondays-and-tuesdays-for-the-next-year) – John Conde Apr 12 '12 at 15:09

1 Answers1

3

The duplicate listed in the comments ( Getting all dates for Mondays and Tuesdays for the next year ) has a rather verbose answer. Here is a succinct solution using strtotime with Relative Formats:

// Initialize our dates to UNIX timestamps
$startDate = strtotime( 'next Tuesday', strtotime($startDate) );
$endDate= strtotime($endDate);

// Output all Tuesdays prior to our end date
while ($startDate < $endDate) {

    echo date('n/j/Y', $startDate ). "\n";

    // Get next Tuesday relative to last Tuesday
    $startDate = strtotime( 'next Tuesday', $startDate );
}

Using today ($startDate) and 2 months from today ($endDate) as an example, the above outputs:

4/17/2012
4/24/2012
5/1/2012
5/8/2012
5/15/2012
5/22/2012
5/29/2012
6/5/2012

See it in action.

Note: Ensure you are using a proper date format to receive accurate results.

Community
  • 1
  • 1
Josh
  • 8,082
  • 5
  • 43
  • 41