2

我有下面的语法,它填充了一个带有周期间的下拉框。它的范围从 3 个月前到 1 年前。

<select id="period" name="period" onchange="updateperioddiv();"  class="tekwani-input-select"> 

        <?php
          for($i = 0; $i <= 445; $i ++){

            $startdate = strtotime("today + $i day - 90 day");
            $enddate = strtotime("today + " . ($i + 6) . " day - 90 day ");

            if(date('D', $startdate) == 'Mon'){

                echo '<option'.' value="'. date('Y-m-d', $startdate) . ' ">' .date('d M y', $startdate) . " to " . date('d M y', $enddate) . "</option>";
            }
          }
        ?> 
   </select>

所以这默认为 3 个月前,我怎样才能让它默认选择为当前周?

php fiddel在这里

谢谢,

4

2 回答 2

1

strtotime() 返回以数字表示的 Unix 时间戳,因此您可以对这些值进行简单的逻辑比较以确定今天是否在特定的一周内。

// Get unix time of today
$today = strtotime("today");

for($i = 0; $i <= 445; $i ++){

    $startdate = strtotime("today + $i day - 90 day");
    $enddate = strtotime("today + " . ($i + 6) . " day - 90 day "); 

    if(date('D', $startdate) == 'Mon'){

        echo '<option ';
        // check to see if today is inside this week
        if( $startdate < $today && $enddate > $today ){
            echo ' selected="selected"';
        }

        echo ' value="'. date('Y-m-d', $startdate) . ' ">' .date('d M y', $startdate) . " to " . date('d M y', $enddate) . "</option>";

    }
}
于 2013-10-22T04:29:28.457 回答
0

你可以这样尝试-

<select id="period" name="period" onchange="updateperioddiv();"  class="tekwani-input-select"> 

        <?php
            echo "week".date("Y",$startdate);
          for($i = 0; $i <= 445; $i ++){

            $startdate = strtotime("today + $i day - 90 day");
            $enddate = strtotime("today + " . ($i + 6) . " day - 90 day ");

            if(date('D', $startdate) == 'Mon'){
                $selected = "";
                if(date('W', $startdate) == date('W'))
                   $selected = "selected";
                echo '<option'.' value="'. date('Y-m-d', $startdate) . ' " $selected>' .date('d M y', $startdate) . " to " . date('d M y', $enddate) . "</option>";
            }
          }
        ?> 
   </select>
于 2013-10-22T04:33:59.443 回答