Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Insert date into mysql

I am trying to insert date into mysql but everytime it fails and comes out as 0000-00-00 in phpmyadmin

My date format is like 2012-08-06 (yyyy-mm-dd) and the date field type in database is date.

$date = "2012-08-06";
mysql_query("INSERT INTO data_table (title, date_of_event) 
             VALUES('". $_POST['post_title'] ."',
                    '". $date ."')") or die(mysql_error()); 

tried changing - to / or removing them, it doesn't work.

like image 292
xperator Avatar asked Aug 06 '12 11:08

xperator


People also ask

How can I insert current date and time in MySQL using PHP?

The simplest method to insert the current date and time in MySQL is to use the now() function. Once you call the function, it returns the current date and time in the system's configured time zone as a string. The value returned from the now() function is YYYY-MM-DD for the date and HH-MM-SS-UU for the time record.

How do I insert date in YYYY-MM-DD format in MySQL?

Introduction to MySQL DATE data type This format is fixed and it is not possible to change it. For example, you may prefer to use mm-dd-yyyy format but you can't. Instead, you follow the standard date format and use the DATE_FORMAT function to format the date the way you want. MySQL uses 3 bytes to store a DATE value.

How do I insert the current date in a column in SQL?

To insert only date value, use curdate() in MySQL. With that, if you want to get the entire datetime, then you can use now() method. Insert both date and time with the help of now().


1 Answers

try

$date = "2012-08-06";
$date=date("Y-m-d",strtotime($date));
like image 56
WatsMyName Avatar answered Nov 15 '22 14:11

WatsMyName