简体   繁体   English

要求用户使用YYYY-MM-DD格式输入日期

[英]Ask Users to Input the Date using the YYYY-MM-DD format

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. Mysql的格式为YYYY-MM-DD,所以我决定使用它。 Now I need to ask the users to input using the YYYY-MM-DD format. 现在,我需要请用户使用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? 如何使用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 - - . 另外,在不需要编辑连字符(-)的情况下该怎么做...我的意思是它将保持原样,并且用户只需输入数字- -

Your help would be highly appreciated. 您的帮助将不胜感激。 Thank you. 谢谢。

I would recommend using a more user-friendly solution like for example a jQuery Datepicker . 我建议使用更用户友好的解决方案,例如jQuery Datepicker

You can set the date-format to the format you want without having to bother your users. 您可以将日期格式设置为所需的格式,而不必打扰用户。

You can format a date, in PHP, using the date() function : 您可以使用date()函数在PHP中格式化日期:

$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 : 然后,要预先填写表单中的数据,只需将该日期放入<input>字段的value属性中即可:

<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 . 例如jQuery的Datepicker
  • In any case, don't forget to validate the user input : 无论如何,请不要忘记验证用户输入:
    • Correct format 正确格式
    • Valid date 有效日期

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. 我也建议您使用日期选择器,但您也可以将日期框拆分为3个选择框,分别是1-31、1-12、2011-2015等,因此用户可以从框中选择日期。 I prefer the Date Picker one though. 我更喜欢日期选择器。 You can then format the data in any way you want. 然后,您可以按任何需要的格式格式化数据。

您必须使用输入类型作为日期格式:

<input type="date" name="event_startDate">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正则表达式使用格式为YYYY-MM-DD在PHP中验证日期 - Regex to validate date in PHP using format as YYYY-MM-DD 如何解析格式为“ yyyy-mm-dd到yyyy-mm-dd”的字符串范围以开始/结束日期 - How to parse a string range in the format “yyyy-mm-dd to yyyy-mm-dd” to start/end date 验证输入是YYYY-MM-DD HH:mm:ss格式? - Validate input is in YYYY-MM-DD HH:mm:ss format? 在laravel中以mysql date格式插入日期为yyyy-mm-dd - Insert a date with format of mysql date as yyyy-mm-dd in laravel 如何在PHP中比较格式为yyyy-mm-dd的日期字符串 - How to compare date strings in the format yyyy-mm-dd in PHP PHP 检查日期的正则表达式为 YYYY-MM-DD 格式 - PHP Regex to check date is in YYYY-MM-DD format 在laravel刀片中将引导日期格式设置为yyyy-mm-dd - Setting bootstrap date format to yyyy-mm-dd in laravel blade 如果日期字符串从dd / mm / yyyy转换为yyyy-mm-dd,如何有条件地重新格式化用户输入? - How to conditionally reformat user input if a date string of dd/mm/yyyy to yyyy-mm-dd? 日期格式 - DD-MM-YYYY 到 YYYY-MM-DD - Date formatting - DD-MM-YYYY to YYYY-MM-DD 我正在尝试在输入类型日期中将日期格式mm / dd / yyyy转换为dd / mm / yyyy - I am trying date format mm/dd/yyyy to dd/mm/yyyy in input type date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM