You need to replace "/" by "-" to get the correct format
$chq_date = str_replace('/', '-', $chq_date );
echo $date = date("Y-m-d", strtotime($chq_date));
Pergunta
I wrote a line to convert the date entered from a view to be converted into date format in the database. But the function is not working (PHP version 5.4). Here's the code;
$chq_date = $_POST['cheque_date'];
echo $date = date("Y-m-d", strtotime($chq_date));
The value obtained for $chq_date
is 24/01/2014
, but then the $date
is showing the result 1970-01-01
. How does this happens? What is wrong with the script?
If someone could please help me out..
Solução
You need to replace "/" by "-" to get the correct format
$chq_date = str_replace('/', '-', $chq_date );
echo $date = date("Y-m-d", strtotime($chq_date));
Outras dicas
change the accpeted date format to: (value for $_POST['cheque_date'])
24-01-2014
remove / and add - in dates... then you will get the correct result.
You can try it.
$chq_date = str_replace('/', '-', $_POST['cheque_date'];);
echo $date = date('Y-m-d', strtotime($chq_date));
$chq_date = str_replace("/","-","24/01/2014");
echo $date = date("Y-m-d", strtotime($chq_date));
this is the format to use strtotime(). So for Your example , You use below:-
$chq_date = str_replace("/","-",$_POST['cheque_date']);
echo $date = date("Y-m-d", strtotime($chq_date));
if the separator in Your $_POST['cheque_date'] is / otherwise use proper separator. check here for more details on str_replace().
Your giving date format as DD/MM/YYYY
It must be as MM/DD/YYYY
$chq_date = "11/13/14";
echo date("jS F, Y", strtotime($chq_date));