2

如何在 php.ini 中创建本周和前几周最多四个的下拉导航。 在此处输入图像描述

$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";
}

此代码用当前周填充下拉列表,但我想要的是用前四个星期填充下拉列表。

4

2 回答 2

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)
于 2012-04-23T10:31:13.707 回答
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));
}
于 2014-01-31T22:39:48.507 回答