我正在为客户建立一个网站(我是一名试图学习编码的设计师)并且有三个选择。
它看起来像这样:
现在客户希望到达日期反映所有选择中的当前日期。我怎样才能用 PHP 做到这一点?
感谢大家的帮助!
使用日期时间
$date = new DateTime();
$day = $date->format('d');
$month = $date->format('m');
$year = $date->format('Y');
然后当你生成你的选择
<select name="day">
<?php
for($i = 1; $i <= 31; $i++)
{
printf('<option value="%d" %s>%d</option>', $i, $i == $day ? 'selected="selected"' : '', $i);
}
?>
</select>
<?php
$date = new DateTime();
$months = array(1 => 'Jan', 2 => 'Feb', 3 => 'Mar', 4 => 'Apr', 5 => 'May', 6 => 'Jun', 7 => 'Jul', 8 => 'Aug', 9 => 'Sep', 10 => 'Oct', 11 => 'Nov', 12 => 'Dec');
?>
<select name="month">
<?php foreach($months as $key => $month) { ?>
<?php $default_month = ($key == $date->format('m'))?'selected':''; ?>
<option <?php echo $default_month; ?> value="<?php echo $key; ?>">
<?php echo $month; ?>
</option>
<?php } ?>
</select>
<select name="day">
<?php for($day = 1; $day <= 31; $day++) { ?>
<?php $default_day = ($day == $date->format('d'))?'selected':''; ?>
<option <?php echo $default_day; ?> value="<?php echo $day; ?>">
<?php echo $day; ?>
</option>
<?php } ?>
</select>
<select name="year">
<?php for($year = $date->format('Y'); $year <= 2020; $year++) { ?>
<option value="<?php echo $year; ?>">
<?php echo $year; ?>
</option>
<?php } ?>
</select>