How would I put together a PHP5 function that would find the current calendar week and return the dates of each day in the week as an array, starting on Monday? For example,
I suppose a solution would be to start by getting the timestamp that correspond to last monday, using strtotime :
$timestampFirstDay = strtotime('last monday');
But if you try with today (thursday), with something like this :
$timestampFirstDay = strtotime('last thursday');
var_dump(date('Y-m-d', $timestampFirstDay));
you'll get :
string '2010-02-18' (length=10)
i.e. last week... For strtotime, "last" means "the one before today".
Which mean you'll have to test if today is "last monday" as returned by strtotime plus one week -- and, if so, add one week...
Here's a possible (there are probably smarter ideas) solution :
$timestampFirstDay = strtotime('last monday');
if (date('Y-m-d', $timestampFirstDay) == date('Y-m-d', time() - 7*24*3600)) {
// we are that day... => add one week
$timestampFirstDay += 7 * 24 * 3600;
}
And now that we have the timestamp of "last monday", we can write a simple for loop that loops 7 times, adding 1 day each time, like this :
$currentDay = $timestampFirstDay;
for ($i = 0 ; $i < 7 ; $i++) {
echo date('Y-m-d', $currentDay) . '
';
$currentDay += 24 * 3600;
}
Which will give us this kind of output :
2010-02-22
2010-02-23
2010-02-24
2010-02-25
2010-02-26
2010-02-27
2010-02-28
Now, up to you to :
for loop so it stores the dates in an arrayHave fun ;-)