dongmeba4877 2012-04-23 10:13
浏览 9
已采纳

如何在php中创建前四周填充的下拉列表

How to create a drop down navigation of the current week and previous weeks up to four in php. enter image description here

$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.

  • 写回答

2条回答 默认 最新

  • duanjuelian4640 2012-04-23 10:31
    关注
    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?