如何在选择当前日,月,年中显示?

时间:2014-01-12 12:43:48

标签: php date

今天我决定为我的项目更新报告选项,我想在此选择中显示当前日,月,年:

$days = range(1,31);
echo"<select style=\"float:left; width:auto;\" name=\"day1\">";
foreach($days as $day){
echo "
<option value=$day>$day</option>
" .PHP_EOL ;
}
echo "</select>";


$months = array(1=>'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ); 
echo"<select style=\"float:left; width:auto;\" name=\"month1\">";
foreach($months as $key=> $month){
echo "<option value=$key>$month</option>" .PHP_EOL ;
}  
 echo "</select>";

 $this_year = date("Y");
$span = $this_year - 4;
$years = range($this_year, $span);
echo"<select style=\"float:left; width:auto;\" name=\"year1\">";
foreach($years as $year){
echo "<option value=$year>$year</option>" .PHP_EOL ;
}  
 echo "</select>";

如何选择当前日,月,年?

4 个答案:

答案 0 :(得分:0)

使用date()

if(date("Y") == $year) { echo " selected"; }

所以

foreach($years as $year){
 echo "<option value=$year>$year</option>" .PHP_EOL ;
} 

变为

foreach($years as $year){
 echo "<option";
 if(date("Y") == $year) { echo " selected"; }
 echo ">$year</option>" .PHP_EOL ;
} 

答案 1 :(得分:0)

// For Days
// ---------------------------------------
$days = range(1,31);

echo '<select style="float:left; width:auto;" name="day1">';

foreach($days as $day){
    if ($day == date('d') )
        printf('<option value="%d" selected="selected">%d</option>', $day, $day);
    else
        printf('<option value="%d">%d</option>', $day, $day);
}

echo "</select>";
// ---------------------------------------



// For Months 
// ---------------------------------------
$months = array( 1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ); 

echo '<select style="float:left; width:auto;" name="month1">';

foreach($months as $key => $month ){
    if ($key == date('m') )
        printf('<option value="%d" selected="selected">%d</option>', $key, $month);
    else
        printf('<option value="%d">%d</option>', $key, $month);
}

echo "</select>";
// ---------------------------------------



// For Years
// ---------------------------------------
$this_year = date("Y");
$span      = $this_year - 4;
$years     = range($this_year, $span);

echo '<select style="float:left; width:auto;" name="year1">';

foreach( $years as $year ){
    if ($year == date('Y') )
        printf('<option value="%d" selected="selected">%d</option>', $year, $year);
    else
        printf('<option value="%d">%d</option>', $year, $year);
}

echo "</select>";

答案 2 :(得分:0)

把这个条件放在foreach中 如果(日期( 'd')== $天){

$ selected =“selected”;

}其他{

$ selected =“”;

}

并在选项标记

中使用$ selected变量

答案 3 :(得分:0)

使用一天

 foreach ($days as $day) {
if ($day == date('d'))
    echo "<option selected='selected' value=$day>$day</option>" . PHP_EOL;
else
    echo "<option value=$day>$day</option>" . PHP_EOL;
}

月份

foreach ($months as $key => $month) {
if ($month == date('F'))
    echo "<option selected='selected' value=$key>$month</option>" . PHP_EOL;
else
    echo "<option value=$key>$month</option>" . PHP_EOL;
}

和年份

foreach ($years as $year) {
if ($year == date('Y'))
    echo "<option selected='selected' value=$year>$year</option>" . PHP_EOL;
else
    echo "<option value=$year>$year</option>" . PHP_EOL;
}