是否有一个函数可以将日期转换为如下格式:
31/07/2014改为格式日期,如2014-07-31
我想这样做,因为在我的sql查询中,当我将日期以"/"格式输入时,它不理解格式:/
我有这个消息:Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '30/07/2014' for column 'date_recu' at row 1' in C:wampwwwajouter_lot2.php on line 76
在我的SQL表中,日期类型是datetime。
查询$bdd->query('insert into LOT (id_lot,num_ref,date_recu,qty,remarque1,remarque2)
VALUES
(NULL, "'.$Num_Ref.'","'.$Date_Recu.'",''.$Qty.'',"'.$Rem1_Lot.'","'.$Rem2_Lot.'")');
Where $Date_Recu = "31/07/2014"
strtotime()
$Date_Recu = strtotime($Date_Recu); /* shows 31/07/2014 */
$Date_Recu = date('Y-m-d', $Date_Recu);
echo 'Date Recu : '.$Date_Recu; /* shows 2014-07-31 */
您需要将日期转换为MySQL可以理解的格式。
$Date_Recu = date_create_from_format('d/m/Y', $Date_Recu)->format('Y-m-d');
你不应该对d/m/Y格式的日期使用strtotime,因为它无法知道该格式实际上是d/m/Y还是m/d/Y。使用像date_create_from_format这样可以指定格式的函数
你可以试试:-
SELECT DATE_FORMAT(NOW(),'%Y-%m-%d')
结果为2014-07-31.