使用日期函数以mysql日期格式格式化日期

使用日期函数以mysql日期格式格式化日期

问题描述:

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?

我正在尝试使用php将日期传递给mysql查询,但我没有得到我想要的东西 p>

我的php语句就是那样 p>

  $ date_s =“01/03/2012”; 
 $ date_s = date(“YYYY-MM-  DD“,$ date_s); 
echo $ date_s; 
  code>  pre> 
 
 

打印 1970197019701970-JanJan-ThuThu code> p> \ n

我想要的是将上述日期格式从 01/03/2012 code>格式化为 2012-03-01 code> p>

i 知道格式化日期的小事,但我不知道该怎么做,我已经尝试了所有可能的功能和格式化? p> div>

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));
    ?>