如何在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) . "\n";
}
此代码使用当前周填充下拉列表,但我想要的是填充前四周的下拉列表。
答案 0 :(得分:3)
for ($i = 0; $i <= 4; $i++)
{
$weeks[] = date('m/d/Y', strtotime("-$i week", time()));
}
给出:
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)
编辑:如果您想要范围,请执行以下操作:
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"));
}
给出:
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)
答案 1 :(得分:1)
如果你想在星期一开始你的星期(并在星期五结束),你可以这样做:
$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));
}