I have two datepickers in format dd/mm/YYYY. Problem is that when I store them to database, they got buggy. Output is "1970-01-01'. I am also using mySQL STR_TO_DATE function and php built in function for date format... I don't know why my dates are not working. Can someone told me why my dates got buggy?
Here is code that I am using:
$datumDO=$_POST['datepicker'];
$datumOD=$_POST['datepicker1'];
$time = strtotime($datumDO);
$NewDatum = date('d/m/Y',$time);
$time = strtotime($datumOD);
$NewDatumOD = date('d/m/Y',$time);
if ($stmt = $dbConnection->prepare("INSERT INTO `cat-item` (id_cat, id_item, datumOD, datumDO) VALUES (?,?,STR_TO_DATE(?,'%d/%m/%Y'),STR_TO_DATE(?,'%d/%m/%Y'))")){
$stmt->bind_param('iiss', $category, $item, $NewDatum , $NewDatumOD;
$stmt->execute();
$stmt->close();
}
$datumDO= date('Y-m-d', strtotime($_POST['datepicker']));
$datumOD= date('Y-m-d', strtotime($_POST['datepicker1']));
if ($stmt = $dbConnection->prepare("INSERT INTO `cat-item` (id_cat, id_item, datumOD, datumDO) VALUES (?,?,?,?)")){
$stmt->bind_param('iiss', $category, $item, $datumDO, $datumOD;
$stmt->execute();
$stmt->close();
}
try this.....hope this helps
If your datepickers are providing dates in dd/mm/YYYY
format (with /
as the separator), the inputs to your functions are incorrect.
strtotime:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates [ source ]