php 简化日期的下拉表单输入:日月年 <html>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16241718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simplifying a drop down form input for date: day month year <html>
提问by ForgottenOne
There are 29+ days in a month, I am creating a form using select and option. But this means I will need 31 options for day, 12 for month and a bunch more for the year.
一个月有 29 天以上,我正在使用 select 和 option 创建一个表单。但这意味着我每天需要 31 个选项,一个月需要 12 个选项,一年需要更多选项。
Is there a better way to do it? I'm not looking for plug-ins, I am trying to make some tidy code to replace some old and messy code. Can I use a PHP for loop? And how would I go about doing this? What would it look like (in terms of mixing up the PHP and HTML variables and functions)?
有没有更好的方法来做到这一点?我不是在寻找插件,我是在尝试制作一些整洁的代码来替换一些旧的杂乱的代码。我可以使用 PHP for 循环吗?我该怎么做呢?它会是什么样子(就混合 PHP 和 HTML 变量和函数而言)?
回答by Marcelo Rodovalho
This is not the best way to do this, but is what your asking. Look:
这不是执行此操作的最佳方法,而是您的要求。看:
<select name="d">
<?php for ($i = 1; $i <= 31; $i++): ?>
<option value="<?php echo str_pad($i, 2, '0', STR_PAD_LEFT) ?>"><?php echo $i ?></option>
<?php endfor ?>
</select>
<select name="m">
<?php for ($i = 1; $i <= 12; $i++): ?>
<option value="<?php echo str_pad($i, 2, '0', STR_PAD_LEFT) ?>"><?php echo $i ?></option>
<?php endfor ?>
</select>
and if you need to take the number of the days in the current/specific month use
如果您需要获取当前/特定月份的天数,请使用
$current_month = date('m');
$number_of_day_in_month = cal_days_in_month(CAL_GREGORIAN, $current_month , date('Y'));
回答by Alex Shesterov
If you are using HTML5, then create an <input type="date">
.
Safari, Opera and Chrome already do support this input type. In browsers with no date-input-support, such an input degrades to a simple text field.
如果您使用的是 HTML5,则创建一个<input type="date">
. Safari、Opera 和 Chrome 已经支持这种输入类型。在没有日期输入支持的浏览器中,这样的输入降级为简单的文本字段。
- The specs: http://www.w3.org/TR/html-markup/input.date.html
- Browser support: http://caniuse.com/#feat=input-datetime
You MUST implement validation on server-side (in PHP in your case) anyway - so text field is not a tragedy. You may also use simpleJavaScript to validate user input before submitting the form data as well.
无论如何,您必须在服务器端(在您的情况下使用 PHP)实现验证 - 所以文本字段不是悲剧。您也可以在提交表单数据之前使用简单的JavaScript 来验证用户输入。
If you want nice calendar widget in all (A-grade-)browsers, you'll needto use a plugin or implement this yourself in JavaScript.
如果您想在所有(A 级)浏览器中使用漂亮的日历小部件,您需要使用插件或在 JavaScript 中自己实现。
回答by Dusty
If your trying to simply use HTML (no plugin), this should get you started:
如果您尝试简单地使用 HTML(无插件),这应该让您开始:
<select name="Month">
<option value="January">January</option>
<option value="February">February</option>
<option selected value="March">March</option>
</select>
<select name="Day">
<option value="1">1</option>
<option value="2">2</option>
<option selected value="3">3</option>
</select>
<select name="Year">
<option value="2013">2013</option>
<option value="2012">2012</option>
<option selected value="2011">2011</option>
</select>
Here's the jsfiddle
这是jsfiddle
But I would recommend a JavaScript solution, like jQuery's datepicker (http://jqueryui.com/datepicker/).
但我会推荐一个 JavaScript 解决方案,比如 jQuery 的 datepicker ( http://jqueryui.com/datepicker/)。