1

我创建了一个下拉选择列表,它根据当前日期生成日期列表,

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>
4

4 回答 4

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>';
?>
于 2012-11-16T22:01:41.433 回答
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>';
于 2012-11-16T21:59:11.393 回答
0

您应该做的是为您的计算创建一个函数,以便您可以在代码中使用mydate(+1),mydate(-2)而不是那个意大利面条。

不要在你的 html 中混合(和重复)这么多 php 代码,这是非常糟糕的做法并且难以维护。

其次,在您的函数中,您可以使用适当的函数将时间转换为unix 时间戳,添加毫秒数并将其转换回您需要的任何时间格式。

于 2012-11-16T21:59:30.667 回答
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);
    }
?>
于 2012-11-16T22:00:54.853 回答