我创建了一个下拉选择列表,根据当前日期
生成日期列表 4天前和7天前,我已经尝试了几个较短的脚本,但它们都失败了,我一直在使用以下但它有一些限制......谢谢!<option value=""> select </option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-4),date("Y")));?>" style="color:red;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-4),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-3),date("Y")));?>" style="color:red;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-3),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-2),date("Y")));?>" style="color:red;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-2),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-1),date("Y")));?>" style="color:red;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-1),date("Y")));?></option>
<option value="<?php echo date ('m/d/Y');?>"><?php echo date ('m/d/Y');?> TODAY </option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+1),date("Y")));?>" style="color:green;font-weight:bold;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+1),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+2),date("Y")));?>" style="color:green;font-weight:bold;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+2),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+3),date("Y")));?>" style="color:green;font-weight:bold;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+3),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+4),date("Y")));?>" style="color:green;font-weight:bold;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+4),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+5),date("Y")));?>" style="color:green;font-weight:bold;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+5),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+6),date("Y")));?>" style="color:green;font-weight:bold;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+6),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+7),date("Y")));?>" style="color:green;font-weight:bold;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+7),date("Y")));?></option>
答案 0 :(得分:2)
这应该做你所要求的。我重写了你的代码,使其尽可能简单易读。
<?php
echo '<select>';
echo '<option value="">select</option>';
for($i = -4; $i <= 7; ++$i)
{
$date = date('m/d/Y', strtotime($i.' days'));
echo '<option value="'.$date.'" style="color:red;">'.$date.'</option>';
}
echo '</select>';
?>
答案 1 :(得分:0)
这应该足够了:
echo '<select><option> select </option>';
for($x=-4;$x<=7;$x++){
$date = date("m/d/Y",(time() + (86400 * $x)));
if($x<0){
$color='red';
}elseif($x==0){
$color = 'black;font-weight:bold';
}else{
$color = 'green';
}
echo '<option style="color:'.$color.'" value="'.$date.'">'.($x==0 ? 'TODAY':$date).'</option>';
}
echo '</select>';
答案 2 :(得分:0)
你应该做的是为你的计算创建一个函数,
这样您就可以在代码中使用mydate(+1)
,mydate(-2)
而不是spaggheti。
不要在你的HTML中混合(并重复)这么多php代码,这是非常糟糕的做法,很难维护。
第二次,在您的功能中,您可以将时间转换为具有相应功能的 unix时间戳,添加毫秒数并将其转回到您需要的任何时间格式。
答案 3 :(得分:0)
你试过吗
<?php
for($i = -4; $i <= 7; $i++) {
$date = date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-$i),date("Y")));
$date_txt = $date;
if($i == 0) {
$date_txt = "Today";
}
printf("<option value=\"%s\" style=\"color: red\">%s</option>", $date, $date_txt);
}
?>