我有以下语法,其中填充了一个带有周期的下拉框。它的范围从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 here
谢谢,
答案 0 :(得分: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>";
}
}
答案 1 :(得分: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>