Problem with inserting date into MySQL using PHP post form variable

I have an HTML form using the jquery datepicker to insert the date into a MySQL database using PHP post form variable. Everytime I try to insert the date it inserts the date as 0000-00-00. I've been looking around for the solution and I can't seem to find it. My code is below.

PHP:
$release_date=$_POST['release_date'];
$date=strtotime($release_date);

HTML:
<input type="text" name="release_date" id="datepicker" size="40" required>

SQL:
$sql = "INSERT INTO books(title, description, price, release_date, quantity, active) VALUES ('$title','$description','$price',$date,'$quantity','$active');";

The release_date field in my database table is set to DATE.
kenjpeteAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
Why not use now() and let the server time apply.  Rather than trying to convert something that might not be in the correct format to be converted.  You are not validating so my guess is that the data provided does not match the format.
If the user must insert, you shou,d make sure the format of the date provided is correct.
0
Dave BaldwinFixer of ProblemsCommented:
From http://php.net/manual/en/function.date.php ...
$today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (the MySQL DATETIME format)
You can shorten it to this for just DATE.
$today = date("Y-m-d");                   // 2001-03-10 (the MySQL DATE format)
MySQL is very picky about the format for a DATE field.  Which is why I always use dropdowns instead of allowing users to type in unformatted dates.
0
Ray PaseurCommented:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

If you define the MySQL column as DATE or DATETIME you can format the Unix Timestamp with PHP date('c').  This will give you the ISO-8601 standard format for DATETIME values, and the information will play well with MySQL.

PHP strtotime() provides a return value.  Your script cannot blindly use the return value - it must test for FALSE which indicates a failure to convert the date to the Unix Timestamp.  The article explains how to make this test and what to do with invalid external data.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Brian TaoSenior Business Solutions ConsultantCommented:
Um.... have you tried wrapping the $date variable with single quotes?
$sql = "INSERT INTO books(title, description, price, release_date, quantity, active) VALUES ('$title','$description','$price','$date','$quantity','$active');";

Open in new window

0
kenjpeteAuthor Commented:
Ok a combination of things worked.....first here is the code:

PHP:
$release_date=strtotime($_POST['release_date']);
$date=date('c', $release_date);

SQL:
$sql = "INSERT INTO books(title, description, price, release_date, quantity, active) VALUES ('$title','$description','$price','$date','$quantity','$active');";

Ray that article was spot on....using the date('c') function properly formatted the date, and Brian you were correct, I need to wrap the $date in single quotes in my SQL statement.
0
Ray PaseurCommented:
I might look at it just a little differently (pidgin code):

PHP:
$release_date=strtotime($_POST['release_date']);
if (!$release_date)
{
    trigger_error('Invalid Date: ' . $_POST['release_date'], E_USER_WARNING); // TEST FOR FAILURE!
}
else
{
    $date=date('c', $release_date);
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.