I've a problem in MySql when I try to insert in my database a date.
My table have a datetime column and, when I insert some values with PHP like
INSERT INTO table (column1,column2) VALUES ('1','2010-11-20');
[Column2 is a DATETIME]
My result, in my database is '1' and '0000-00-00 00:00:00' and I don't know why because this worked.... :S.
What can be the problem?
Sorry...I explain more my problem
I read this values from a file with the format '20-11-2010' and I change to the MySql format (it is easy) and write my Insert sentence.
My Insert sentence is good, I put this in other file (like .log) and the result is:
INSERT INTO telephones (telephone,date) VALUES ('123456789','2014-18-11');
But if I look my database, the result is:
'123456789' and '0000-00-00 00:00:00'
Edit:
But in some cases, I've values like '2010-11-20 16:30:20'
. If I change my DATETIME for DATE, I will lose this value.
My data value come from a File and the value is like
'123456789''20-11-2010 16:00:00' [First value is a telephon number and the next is the date]
But in some cases, I recive the date without the time.
When I reed the dates, I changed the value using explode() and I return the value in the next order
$myDate = $date[2]."-".$date[1]."-".$date[0];
When I construct my insert I try the @entiendoNull solution like
date("Y-m-d H:i:s", strtotime($myDate));
but this put in my insert sentence "1970-01-01 XX:xx:xx"
You try to insert info in a DATE format into a column accepting DATETIME.
Concluding, you either have to change your column in your table to a "DATE", or (better) just insert 00:00:00 as time:
INSERT INTO table (column1,column2) VALUES ('1','2010-11-20 00:00:00');
Format your date the following way and then try to insert it:
$formattedDate = date("Y-m-d H:i:s", strtotime($value)); //$value holds a date e.g. 2015-01-19 or a datetime e.g. 2015-01-19 10:58:01