0

Hi I have a form to add members with their birth date. The input field for birth date is:

<div class="form-group">
   <label for="data_nascita" class="col-sm-3 control-label">Birth Date</label>
       <div class="col-sm-9">
        <input name="data_nascita" type="date" class="form-control" id="data_nascita" placeholder="gg/mm/aaaa"  />
       </div>
</div>

Data are correctly uploaded to MYSQL and date format is

$data_nascita = ($_POST['data_nascita']);
var_dump($data_nascita); => string(10) "2003-04-15"

In the database is stored correctly, and it appears as

2018-12-14 18:50:48

I want to have the possibility to edit the information about the person (i.e. changing the birth date), and I created an edit file where all the database information is retrieved and appears in form fields that can be edited and updated in MYSQL.

Everything works fine except for dates, which appears as gg/mm/aaaa

The code I used for retrieving data is as usual:

<?php
    $query = "SELECT data_nascita FROM volontari WHERE id_volontari = '$id'";
       $res = $mysqli ->query($query);
       $row = $res->fetch_array (MYSQLI_ASSOC);
       $data_nascita = gira_data_db($row['data_nascita']);

    function gira_data_db($data)
    {
        $array = explode( "-" , $data);
        $array2 = array($array[2], $array[1], $array[0]);
        $testo = implode( "/" , $array2);
        return $testo;
    }
?>
<div class="form-group">
 <label for="data_nascita" class="col-sm-3 control-label">Birth Date</label>
    <div class="col-sm-9">
      <input name="data_nascita" type="date" class="form-control" id="data_nascita" value="<?php echo $data_nascita ?>" />
    </div>
</div>

The date retrieved is

var_dump(gira_data_db($row['data_nascita']);) => string(10) "15/04/2003" 

However in my form field the data appears as 00/00/0000. If I echo the date in an input field type=text, it appears correct.

Am I doing something wrong?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Elena Politi
  • 161
  • 2
  • 18

1 Answers1

1

The problem is when you explode by '-' array[2] is '14 18:50:48' and it's not valid value for input with date type.

simply change gira_data_db function as follows:

function gira_data_db($data)
{
    return date("YYYY-MM-DD", strtotime($data));
}

I hope it would help you

Mahsa
  • 398
  • 1
  • 3
  • 12
  • I tried your function but it doesn't work yet. It is strange as you are right in theory, but when I var_dump what comes from MYSQL I don't see any time, just the date formatted properly. – Elena Politi Dec 14 '18 at 18:41
  • Just did another check by echoing your function: it gives the proper format date, but still it doesn't appear in my form field – Elena Politi Dec 14 '18 at 18:45
  • According to [link](https://stackoverflow.com/questions/14212527/how-to-set-default-value-to-the-inputtype-date) you need to change date format to YYYY-MM-DD – Mahsa Dec 14 '18 at 18:50
  • I did also what you suggested but nope: same as before – Elena Politi Dec 14 '18 at 18:55
  • what browser are you using? – Mahsa Dec 14 '18 at 18:59
  • Firefox Mozilla – Elena Politi Dec 14 '18 at 18:59
  • Gosh... just realized that on Safari it works... goddammmm! :-D – Elena Politi Dec 14 '18 at 19:01
  • Ok, at this point my question is: why I can see the date in Safari and not in Firefox or in Chrome? I see that the date is properly stored in the MYSQL and properly retrieved from MYSQL. However I don't see it appearing in my input date field on two browser over three. Is there a way to solve it? Thanks a lot! – Elena Politi Dec 17 '18 at 07:49