i am trying to pass date to mysql query using php, but i am not getting what i want
my php statement is like that
$date_s="01/03/2012";
$date_s=date("YYYY-MM-DD", $date_s);
echo $date_s;
its printing 1970197019701970-JanJan-ThuThu
what i want is to format above date from 01/03/2012
to 2012-03-01
i know its little thing to format the date but i am not figuring out what to do, i have tried all possible functions and formatting?
Try this
$date_s="01/03/2012";
$date_s=date("Y-m-d", strtotime($date_s));
echo $date_s;
You could do this in PHP:
$date_s = join('-', array_reverse( explode( '/', $date_s) ) );
better just use str_replace
and date()
functions. it will work.
$date_s="01/03/2012";
$date_s =str_replace("/","-",$date_s);
echo $date_s=date("Y-m-d", strtotime($date_s));
try
$date = new DateTime($date_s);
echo $date->format('Y-m-d');
but if you are passing it to MySQL i would recommend
echo $date->format('c');
$date = '...';
$sqlDate = date('Y-m-d', strtotime($date));
<?php
// this will be helpful for u
$oldDate="22-3-1986"; //any format date
$newDate = date('Y-m-d', strtotime($oldDate));
?>