Datapicker check the age of users and sometimes saves a wrong value in DB

Viewed 46

I am using this tutorial, but I have it slightly modified some fields for my needs, nothing special, just :

$('.txtDate').datepicker({
        changeMonth: true,
        changeYear: true,
        showOn: 'focus',
        showAnim: "fadeIn",
        dateFormat: 'dd/mm/yy',
        autoSize: true,
        yearRange: '1900:+0',
        onSelect: function (dateString, txtDate) {
            ValidateDOB(dateString, txtDate.id);
        }
    });

these are html inputs (I have multiple of them):

 <label class="mr-3" for="c1_dob">Birthday <span>*</span></label>
 <input class="txtDate" type="text" id="c1_dob" autocomplete="off" name="c1_dob" onkeydown="return false;" required/>

the rpoblem is, that sometimes the date is saved to DB as 1970-01-01. At which input will that happen is also not clear. Pure random. can some1 point me, why does it happen? If you need to see more code, don't hesitate to ask.

1 Answers

From your comment:

So that means that ´date('Y-m-d', strtotime($next_array[7]))´ does not work properly.

You use strtotime() with a date format like dd/mm/yyyy... But it expects mm/dd/yyyy. So the day and month are "reversed" in the parsing process.

See PHP documentation strtotime() and Supported Date and Time Formats


Your PHP fix will be to reverse them:

<?php
$dateSplit = explode("/", $next_array[7]);  // "16/03/1983";
$datefixed = $dateSplit[1]."/".$dateSplit[0]."/".$dateSplit[2];
$iso_date = date("Y-m-d", strtotime($datefixed));

echo $iso_date; // Outputs "1983-03-16"
?>
Related