How to set input type date's default value to today?

后端 未结 30 2973
刺人心
刺人心 2020-11-22 14:00

The HTML5 input types are great, Opera\'s new built-in date picker is a breeze, and Chrome has at least supported the new input type with a spin-wheel implementation.

<
30条回答
  •  隐瞒了意图╮
    2020-11-22 14:39

    There is no default method within HTML itself to insert todays date into the input field. However, like any other input field it will accept a value.

    You can use PHP to fetch todays date and input it into the value field of the form element.

    ";
    ?>
    

    The value field accepts the format YYYY-MM-DD as an input so simply by creating a variable $date_string in the same format that the input value accepts and fill it with the year, month and day fetched from todays date and voilá! You have yourself a preselected date!

    Hope this helps :)

    Edit:

    If you would like to have the input field nested within HTML rather than PHP you could do the following.

    
    
        ...
        
            

    I realise this question was asked a while back (2 years ago) but it still took me a while to find a definite answer out on the internet, so this goes to serve anyone who is looking for the answer whenever it may be and hope it helps everyone greatly :)

    Another Edit:

    Almost forgot, something thats been a royal pain for me in the past is always forgetting to set the default timezone whenever making a script in PHP that makes use of the date() function.

    The syntax is date_default_timezone_set(...);. Documentation can be found here at PHP.net and the list of supported timezones to insert into the function can be found here. This was always annoying since I am in Australia, everything is always pushed back 10 hours if I didn't set this properly as it defaults to UTC+0000 where I need UTC+1000 so just be cautious :)

提交回复
热议问题