获取PHP中的Months and Days下拉列表的最佳方法是什么?
我尝试了以下代码,但mktime
已弃用:
for ($j = 1; $j <= 12; $j++)
{
$month_text = date("F", time(0, 0, 0, $j+1, 0, 0, 0));
$month[$j] = $month_text;
}
感谢。
答案 0 :(得分:3)
你使它变得比它需要的复杂得多。只需将月份的名称放在数组中,然后使用range()
创建日期数组。
$months = array("January","February","March","April","May","June","July","August","September","October","November","December");
$days = range(1, 31);
如果您希望月份编号与月份名称一致,则只需在月份数组的开头添加一个空数组元素。
答案 1 :(得分:1)
此问题已在此处提出:How to get all days, months, and years in a drop down?
这就是他所做的,但请记住,他确实使用了mktime,正如你所说的那样。为避免列出年份列表,请执行以下操作:
<?php
// build months menu
echo '<select name="month">' . PHP_EOL;
for ($m=1; $m<=12; $m++) {
echo ' <option value="' . $m . '">' . date('M', mktime(0,0,0,$m)) . '</option>' . PHP_EOL;
}
echo '</select>' . PHP_EOL;
// build days menu
echo '<select name="day">' . PHP_EOL;
for ($d=1; $d<=31; $d++) {
echo ' <option value="' . $d . '">' . $d . '</option>' . PHP_EOL;
}
echo '</select>' . PHP_EOL;
?>