Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Prakhar Patwa
Prakhar Patwa
11,260 Points

how to insert date and time into phpMyAdmin database, taking dynamically from the user through form.

I am facing a huge problem to inserting a date through form as i am working on my "reservation system project" please help me out. If anyone can share their experience. I am not using Database with PDO. Thankyou.

2 Answers

Codin - Codesmite
Codin - Codesmite
8,600 Points

I assume as you are reffering to phpMyAdmin you are using mySQL database.

To get the current timestamp in a format that is accepted by mySQL date type do the following:

<?php
   $date = date("Y-m-d H:i:s");
?>

This will create the variable date in numerical format that mySQL can understand for example: 2016-03-16 10:44:13.

For more info on the paramerters for the PHP date function check this page from the PHP manual: http://www.php.net/manual/en/function.date.php

If you are placing a user entered date into mySQL you can do the following to convert it into the correct format:

<?php
    $date = "2016-03-16 10:44:13"; // String of the users entered date as example, make sure your form collects the date in this format as a string.
    $date = date('Y-m-d H:i:s'); // Convert string to date format.
?>

You might want to check out this post from a year ago: https://teamtreehouse.com/community/date-from-an-form-into-a-php-script-to-enter-into-a-mysql-database

There are also a number of hits on google if you query php insert date from form into mysql, including this one from StackOverflow: http://stackoverflow.com/questions/30102494/trying-to-insert-input-type-date-into-mysql-column-of-type-date-throws-sql-error