【发布时间】:2014-09-23 02:39:26
【问题描述】:
是否有一个函数可以将日期转换为如下格式:
31/07/2014 转换为 2014-07-31 等格式日期
我想这样做是因为在我的 sql 查询中,当我将日期放入带有“/”的格式时,它不理解 te 格式:/
我有这条消息: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:\wamp\www\ajouter_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.'")');
其中 $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 */
【问题讨论】:
-
你应该使用 strtotime() 函数并保存在时间戳中。
-
指定您的查询怎么样?
-
我用我的查询进行了编辑,问题实际上是 $Date_Recu 的格式,我尝试了 substring 函数,但它需要太多的行来格式化:/
-
MySql 工作台。 “strtotime()”似乎工作正常^^
标签: mysql sql date date-format