How to create a drop down navigation of the current week and previous weeks up to four in php.
$date = '04/24/20012';
$ts = strtotime($date);
$year = date('o', $ts);
$week = date('W', $ts);
for($i = 1; $i <= 7; $i++) {
$ts = strtotime($year.'W'.$week.$i);
print date("m/d/Y l", $ts) . "
";
}
This code populate the drop down with current week but what i want is to populate the drop down with previous four weeks.
for ($i = 0; $i <= 4; $i++)
{
$weeks[] = date('m/d/Y', strtotime("-$i week", time()));
}
Gives:
array
0 => string '04/23/2012' (length=10)
1 => string '04/16/2012' (length=10)
2 => string '04/09/2012' (length=10)
3 => string '04/02/2012' (length=10)
4 => string '03/26/2012' (length=10)
EDIT: If you want the range then do this:
for ($i = 0; $i <= 4; $i++)
{
$k = $i - 1;
$weeks[] = date('m/d/Y', strtotime("-$i week")) . ' - ' .
date('m/d/Y', strtotime("-$k week -1 day"));
}
Gives:
array
0 => string '04/23/2012 - 04/29/2012' (length=23)
1 => string '04/16/2012 - 04/22/2012' (length=23)
2 => string '04/09/2012 - 04/15/2012' (length=23)
3 => string '04/02/2012 - 04/08/2012' (length=23)
4 => string '03/26/2012 - 04/01/2012' (length=23)
If you want to start your week on Monday (and end on Friday) you can do something like this:
$this_monday = strtotime('monday this week');
for ($i = 0; $i <= 4; $i++)
{
$k = $i - 1;
$weeks[] = date('m/d/Y', strtotime("-$i week", $this_monday)) . ' - ' .
date('m/d/Y', strtotime("-$k week -3 day", $this_monday));
}