How to return today's type input date?

2

I'm trying to fill an input field with type="date" with the date of the day the form is being filled.

I tried the following code in PHP:

function getDatetimeNow() {
    $tz_object = new DateTimeZone('Brazil/East');
    $datetime = new DateTime();
    $datetime->setTimezone($tz_object);
    return $datetime->format('Y-m-d ');
}

In HTML I did the following:

<input name="name_01" id="id_01" type="date" value="<?php echo getDatetimeNow() ?>" />

You did not print anything, but I also tried JavaScript:

now = new Date; 
yr = now.getFullYear();
mt = now.getMonth();
dy = now.getDay();  
document.getElementById('id_01').innerHTML= yr+"-"+mt+"-"+dy;

Does anyone have any ideas how to bring the completed field?

    
asked by anonymous 05.12.2016 / 15:58

1 answer

3

With php, you can do this:

date_default_timezone_set('America/Sao_Paulo');
...
<input name="name_01" id="id_01" type="date" value="<?php echo date('Y-m-d'); ?>" />

DEMONSTRATION

With javascript:

var today = new Date();
var dy = today.getDate();
var mt = today.getMonth()+1;
var yr = today.getFullYear();
document.getElementById('id_01').value= yr+"-"+mt+"-"+dy;
<input name="name_01" id="id_01" type="text" />
    
05.12.2016 / 16:09