0

我有这个长代码可以在表单下拉列表中显示今天的日期和时间。它确实工作正常,但我想知道是否有更简单的方法来做到这一点......?

我想显示所有月、日、年(只有几年)、所有小时(24 小时格式)、所有分钟和所有秒,但在每个下拉列表中选择当前日期/时间。

<select name="event_month">
<?
$arr_m = array("January","February","March","April","May","June","July","August","September","October","November","December");
$date_m = date(m);
$count = 1;
$months = 12;
do{
    $month = $arr_m[$count-1];
    if ($date_m == $count) {
        echo ("<option value='$month' selected='selected'>$month</option>");
        } else {
        echo ("<option value='$month'>$month</option>");
    }
    $count++;
}while($count<=$months);
?></select>

<select name="start-day">
<?
$date_d = date(d);
$day=1;
$days = 31;
do{
    if($date_d == $day){
        echo ("<option value='$day' selected='selected'>$day</option>");
    }else{
        echo ("<option value='$day'>$day</option>");
    }
    $day++;
}while($day<=$days);
?></select>

<select name="start-year"><?
$date_y = date(Y);
$year =intval($date_y);
$years = $year+2;
do{
    if($date_y == $year){
        echo ("<option value='$year' selected='selected'>$year</option>");
    }else{
        echo ("<option value='$year'>$year</option>");
    }
    $year++;
}while($year<=$years);
?></select>

<select name="start-hour"><?
$date_h = date(H);
$hour = 1;
$hours = 24;
do{
    if($date_h == $hour){
        echo ("<option value='$hour' selected='selected'>$hour</option>");
    }else{
        echo ("<option value='$hour'>$hour</option>");
    }
    $hour++;
}while($hour<=$hours);
?></select>

<select name="start-min"><?
$date_min = date(i);
$min = 1;
$mins = 60;
do{
    if($date_min == $min){
        echo ("<option value='$min' selected='selected'>$min</option>");
    }else{
        echo ("<option value='$min'>$min</option>");
    }
    $min++;
}while($min<=$mins);
?></select>

<select name="start-sec"><?
$date_sec = date(s);
$sec = 1;
$secs = 60;
do{
    if($date_sec == $sec){
        echo ("<option value='$sec' selected='selected'>$sec</option>");
    }else{
        echo ("<option value='$sec'>$sec</option>");
    }
    $sec++;
}while($sec<=$secs);
?></select>

-- 更新 -- 这是更干净的版本 - 谢谢!

<select name="start-month">
<?
$arr_m = array("January","February","March","April","May","June","July","August","September","October","November","December");
$month = date('m');
for ($i = 0; $i <= 12; $i++) {
    $name = $arr_m[$i-1];
    $sel = ($i == $month) ? ' selected="selected"' : '';
    echo "<option value=\"$i\"$sel>$name</option>";
}
?>
</select>


<select name="start-day">
<?
$day = date('d');
for ($i = 0; $i <= 31; $i++) {
    $sel = ($i == $day) ? ' selected="selected"' : '';
    echo "<option value=\"$i\"$sel>$i</option>";
}
?>
</select>

<select name="start-year">
<?
$year = date('Y');
$years = $year+2;
for ($i = $year; $i <= $years; $i++) {
    $sel = ($i == $year) ? ' selected="selected"' : '';
    echo "<option value=\"$i\"$sel>$i</option>";
}
?>
</select>

<select name="start-hour">
<?
$hour = date('G');
for ($i = 0; $i <= 24; $i++) {
    $sel = ($i == $hour) ? ' selected="selected"' : '';
    echo "<option value=\"$i\"$sel>$i</option>";
}
?>
</select>

<select name="start-min">
<?
$min = date('i');
for ($i = 0; $i <= 59; $i++) {
    $sel = ($i == $min) ? ' selected="selected"' : '';
    echo "<option value=\"$i\"$sel>$i</option>";
}
?>
</select>

<select name="start-sec">
<?
$sec = date('s');
for ($i = 0; $i <= 59; $i++) {
    $sel = ($i == $sec) ? ' selected="selected"' : '';
    echo "<option value=\"$i\"$sel>$i</option>";
}
?>
</select>
4

2 回答 2

1

看看你是如何调用date()函数的:

$date_m = date(m);

在您的代码中,您不断传递一个常量,例如m, d, Y。之所以可行,是因为 PHP 在这种情况下非常宽松,它会查找常量,但没有找到它,然后假定您指的是字符串m并将其传递给函数。

尽管代码有效,但您仍然应该通过在传递给的值周围添加引号来解决问题,date()例如:

$date_m = date('m');

这样做的原因是为了防止将来出现任何错误,您可能会定义一个名为m. 例如:

define('m', 'something...');

// here you would be passing something... to the date function and not m
$date_m = date(m);

在简化代码方面,@MarcB 的建议会很好。另一种选择是创建一个函数,它将一个数组和选定的选项作为参数,并生成<select>HTML。这将消除代码中的大量 HTML 重复,因为您无需一遍又一遍地重复下拉 HTML,而是创建一个数组并调用您的函数,例如

echo getHTMLDropdown($options, $selectedOption);
于 2012-10-17T16:48:34.217 回答
1

一些小的优化,以减少 LOC 计数:

for ($i = 0; $i <= 59; $i++) {
    $sel = ($i == $sec) ? ' selected="selected"' : '';
    echo "<option value=\"$i\"$sel>$i</option>";
}

对于其他下拉菜单也是如此。

于 2012-10-17T16:27:08.007 回答