I have a registration form for a website which uses bootstrap datepicker module to handle "birthday" date in a format dd.mm.yyyy.
Input:
<input type="text" id="birthday" name="birthday" data-provide="datepicker" class="form-control datepicker" required />
JS:
$('.datepicker').datepicker({
format: "dd.mm.yyyy",
weekStart: 1,
autoclose: true
});
When a user submits the form, this is how I get the birthday
$user->birthday=$_POST['birthday'];
and in the user creation class, I will sanitize the input with:
$this->birthday=htmlspecialchars(strip_tags($this->birthday));
Now, I want to save this into a MySQL table, and "birthday" column is formatted as DATE, so it has to have the format of Y-m-d. To do this, I tried to convert the date like so:
//since I get a string, first convert it to time, create a date from it and format it to Y-m-d and then bind it to the birthday parameter.
$birthday_converted = date_format(date_create(strtotime($this->birthday)),"Y-m-d");
$stmt->bindParam(':birthday', $this->birthday_converted);
When I do this, I get an error saying this:
Warning: date_format() expects parameter 1 to be DateTimeInterface, integer given
even though, the date will be written into the database, but incorrectly. For example, if the date picked is 16.03.2020 (March 16th), in DB I will get 2016-03-20