php 要求用户使用 YYYY-MM-DD 格式输入日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5687637/
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
Ask Users to Input the Date using the YYYY-MM-DD format
提问by newbie
Good day!
再会!
I am new to programming and I am a little confused how to ask users to input the date in a web page
我是编程新手,我有点困惑如何要求用户在网页中输入日期
I have the form below to ask for dates:
我有下面的表格来询问日期:
My code is as follows:
我的代码如下:
<form action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST">
<input name="event_startDate" type="text" id="event_startDate" size="17">
<input name="event_endDate" type="text" id="event_endDate" size="17">
<input name="SUBMIT" type="submit" id="submit" value="ADD EVENT">
</form>
The format of Mysql is YYYY-MM-DD so I've decided to use it. Now I need to ask the users to input using the YYYY-MM-DD format.
Mysql 的格式是 YYYY-MM-DD 所以我决定使用它。现在我需要让用户使用 YYYY-MM-DD 格式输入。
I can just insert the YYYY-MM-DD in the text box but I want the start date to be the date today so instead I plan to this:
我可以在文本框中插入 YYYY-MM-DD 但我希望开始日期是今天的日期,所以我打算这样做:
My problem is.... I don't know how. How can i insert the date in the textbox with that format using PHP? Can i add a value? But if i add the value, will it change also when the user decided to change the value and press the submit button? Also, how can I do it wherein the hypen (-) is not needed to be edited... I mean it will stay as is and the user just need to input the numbers - -
.
我的问题是......我不知道如何。如何使用 PHP 在该格式的文本框中插入日期?我可以添加一个值吗?但是,如果我添加该值,当用户决定更改该值并按下提交按钮时,它是否也会改变?另外,在不需要编辑连字符 (-) 的情况下,我该怎么做...我的意思是它会保持原样,用户只需要输入数字 - -
。
Your help would be highly appreciated. Thank you.
您的帮助将不胜感激。谢谢你。
回答by jeroen
I would recommend using a more user-friendly solution like for example a jQuery Datepicker.
我建议使用更用户友好的解决方案,例如jQuery Datepicker。
You can set the date-formatto the format you want without having to bother your users.
您可以将日期格式设置为您想要的格式,而不必打扰您的用户。
回答by Pascal MARTIN
You can format a date, in PHP, using the date()
function :
您可以在 PHP 中使用以下date()
函数格式化日期:
$date = date('Y-m-d');
Then, to pre-fill the data in your form, just put that date in the value
attribute of your <input>
field :
然后,要在表单中预先填写数据,只需将该日期放在字段的value
属性中<input>
:
<input type="text" name="date1" value="<?php echo date('Y-m-d'); ?>" />
This will act as default value.
这将作为默认值。
When the form is submitted, you will receive the date that was in the field when the form has been submitted -- be it the default date, or anything else the user could have typed.
提交表单后,您将收到表单提交时字段中的日期——可以是默认日期,也可以是用户可以输入的任何其他日期。
A couple of additional notes :
一些额外的注意事项:
- You might want to use some widget, a bit more user-friendly ; for instance jQuery's Datepicker.
- In any case, don't forget to validate the user input :
- Correct format
- Valid date
- 你可能想使用一些小部件,对用户更友好;例如jQuery 的 Datepicker。
- 无论如何,不要忘记验证用户输入:
- 正确的格式
- 有效日期
回答by gmadd
I'd also recommend using a Date Picker, but you could also split the date box into 3 select boxes, 1-31, 1-12, 2011-2015 etc, so the user can just select the date from the box. I prefer the Date Picker one though. You can then format the data in any way you want.
我还建议使用日期选择器,但您也可以将日期框拆分为 3 个选择框,1-31、1-12、2011-2015 等,这样用户就可以从框中选择日期。我更喜欢日期选择器之一。然后,您可以以任何您想要的方式格式化数据。
回答by shivam
You have to use input type as a date format:
您必须使用输入类型作为日期格式:
<input type="date" name="event_startDate">