简体   繁体   中英

How to set default date value to input of type 'date'?

I have a date value in PHP variable $date which want to show in the input having type date with following code:

<input type="date" value="<?php echo $date;?>" name="date">

I want to show the value which is in $date variable in the input textbox of date. Also, user should be able to pick the value from the date picker, which is working fine, but i want a date should be there. I looked at previous questions, they assign the today's value, but i want to use the value i have in $date variable.

You can do something like

<?php 
$timestamp = time();
$date = date( "Y-m-d", $timestamp );
?>

<input type="date" value="<?php echo $date; ?>" name="date">

Explanation

date( "Ymd", $timestamp ) formats a timestamp to the appropriate format ( eg 2019-11-29 ).

Resources

Try RMo solution or try.

<input name="date" type="date" value="<?php echo date('F d, Y'); ?>" />

Where:

  • F stands for Month in words
  • d stands for days in number,
  • Y stands for year in 4 digit number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM