PHP date showing '1970-01-01 ' after conversion
Solution 1
Replace /
with -
:
$date1 = strtr($_REQUEST['date'], '/', '-');
echo date('Y-m-d', strtotime($date1));
Solution 2
January 1, 1970 is the so called Unix epoch. It's the date where they started counting the Unix time. If you get this date as a return value, it usually means that the conversion of your date to the Unix timestamp returned a (near-) zero result. So the date conversion doesn't succeed. Most likely because it receives a wrong input.
In other words, your strtotime($date1)
returns 0, meaning that $date1
is passed in an unsupported format for the strtotime function.
Solution 3
$date1 = $_REQUEST['date'];
if($date1) {
$date1 = date( 'Y-m-d', strtotime($date1));
} else {
$date1 = '';
}
This will display properly when there is a valid date()
in $date
and display nothing if not.
Solved the issue for me.
Solution 4
$inputDate = '07/05/-0001';
$dateStrVal = strtotime($inputDate);
if(empty($dateStrVal))
{
echo 'Given date is wrong';
}
else{
echo 'Date is correct';
}
O/P : Given date is wrong
AssamGuy
Updated on July 30, 2022Comments
-
AssamGuy almost 2 years
I have a form in which date format is
dd/mm/yyyy
. For searching database , I hanverted the date format toyyyy-mm-dd
. But when Iecho
it, it showing1970-01-01
. The PHP code is below:$date1 = $_REQUEST['date']; echo date('Y-m-d', strtotime($date1));
Why is it happening? How can I format it to
yyyy-mm-dd
?