Something to keep in mind is that the strtotime()
function is very robust and can give you fuzzy information like the second Sunday in March ("Second Sunday March 0"
).
For example, to find the Sunday that marks the second week of this month:
<?php
$month = time();
// Calculate the first day of the month contained in the $month timestamp
$first_day = strtotime(
date(
'Y-m-1 00:00:00',
$month
)
);
// Calculate the sunday opening the week that contains the first day
// of the month (e.g. August 30th, 2009 is the Sunday that corresponds
// to September 1st, 2009)
$pseudo_first_sunday = strtotime(
'First Sunday',
strtotime(
'-1 Week',
$first_day
)
);
var_dump(date( 'Y-m-d', strtotime('Second Week', $pseudo_first_sunday) ));
The output of this script will be (as of 2009-9-14):
string(10) "2009-09-06"
Which is the second week of September (however not the second FULL week, if you want that substitute $first_day
in for $pseudo_first_sunday
).
Just something to keep in mind when playing with dates in PHP.