1

Is there a function wich can convert a date in format like :

31/07/2014 to a format date like 2014-07-31

I want to do this because in my sql query when i put the date in the format with " / " it doesn't understand te format :/

I have this message : 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

in my sql table, the type of the date is in datetime.

Query

$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"

Solution with 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 */
Booba__2012
  • 143
  • 1
  • 3
  • 14

2 Answers2

1

You can try this either:-

SELECT DATE_FORMAT(NOW(),'%Y-%m-%d')

Which will resulted as 2014-07-31.

Ankit Bajpai
  • 13,128
  • 4
  • 25
  • 40
1

You need to convert the date into the format MySQL can understand.

$Date_Recu = date_create_from_format('d/m/Y', $Date_Recu)->format('Y-m-d');

You should not use strtotime on dates in the d/m/Y format because it has no way of knowing if the format is actually d/m/Y or m/d/Y. Use a function like date_create_from_format where can you specify the format as well.

Peter
  • 138
  • 3