I have the following code:
$date1 = (string)$_POST['convocatory_open_start_date']; // "30/04/2015"
$date2 = (string)$_POST['convocatory_open_end_date']; // "31/05/2015"
$startDate = date('Y-m-d', strtotime($date1));
$endDate = date('Y-m-d', strtotime($date2));
but I always get $startDate and $endDate 1970-01-01 Why???
Please Help I have like 3 hours in the same problem.
Thanks
The problem is that if you use / as a separator, strtotime
will assume the format is m/d/Y. So it will not be able to convert it, and it defaults to 1970-01-01.
The easiest solution would be
$startDate = DateTime::createFromFormat('d/m/Y', $date1)->format('Y-m-d');
$endDate = DateTime::createFromFormat('d/m/Y', $date2)->format('Y-m-d');
First check if the posted variables :- $date1 and $date12 are not NULL Because NULL
is interpreted as 0 by the php function strtotime()
, since you are supposed to pass an integer timestamp. A timestamp of 0 means 1-1-1970.
You need to check the posted contents of the posted variables
if(($date1 === NULL) || ($date1 === NULL) ) {
//don't use `strtotime()`
}
If that is not the case and Your variables are posted, the only thing i can think of is that you have problem with the stringTimeFormatting when using the date function and you input format.
$date1 = (string)$_POST['convocatory_open_start_date']; // "30/04/2015"
$date2 = (string)$_POST['convocatory_open_end_date']; // "31/05/2015"
$startDate = date($date1);
$endDate = date($date2);
echo $startDate, '<br/> ' , $endDate;
Hope that adds some to your Quest!