Store date function

How to insert date from form to database like phpMyAdmin and then show on page while selecting from database?

<td class="text-center"><?php echo date("d/m/Y") ?></td>
Nikhil DangeWeb DeveloperAsked:
Who is Participating?
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.

I think this is right but I have not tested it.

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

$id = '';
//  Set ID to whatever you need it to be.

$query = "SELECT date FROM tablename where ID = $id"; 
$result = $conn->query($query);

if ($result->num_rows > 0) {

    // output data of each row
    while($row = $result->fetch_assoc()) {

$date  = date('d/m/Y', strtotime( $row["date"];));

        echo '<td class="text-center">'.$date. '</td>';
} else {
    echo "No Results";


Open in new window

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
mohan singhWeb developerCommented:
If you want to display date from database

follow the step:
1 . Fetch your date from database and fire a query 
$sql = mysql_query($db_config, "SELECT date FROM table";
$row = mysql_fetch_array($sql);

2. Store your data in a variable 
$date = $row['date']; //now $date fetch your data from your database 


<!--And now you can display your date in any Input field like date, textarea , text etc -->
3. Print or echo your variable in input field 
<input type='date'  value="<?php echo $date; ?>" /><!-- in this field your date will display --> 

Open in new window

NerdsOfTechTechnology ScientistCommented:
If the date field is actually called date, date is a reserved word so you will need to add ticks around the field name.

SELECT `date` FROM tablename

Open in new window

Also, use mysqli or PDO for the connection as mysql is deprecated.

Insert date from form to database
You would pull this from $_POST['fieldname'] and sanitize it.
Price Your IT Services for Profit

Managed service contracts are great - when they're making you money. Yes, you’re getting paid monthly, but is it actually profitable? Learn to calculate your hourly overhead burden so you can master your IT services pricing strategy.

NerdsOfTechTechnology ScientistCommented:
I recommend @rgranlund's solution as best, as it uses mysqli.

With these changes it should work:

$query = "SELECT date FROM tablename where ID = $id"; 

Open in new window

$query = "SELECT `datefieldname` FROM tablename where ID = $id"; 

Open in new window

$date  = date('d/m/Y', strtotime( $row["date"];));

Open in new window

$date  = date('d/m/Y', strtotime( $row["date"]));

Open in new window

NerdsOfTechTechnology ScientistCommented:
Besides a few syntax issues, @rgranlund's solution uses mysqli and has the appropriate error checking and looping (for multiple records); therefore, I vote that this expert gets full points.
Nikhil DangeWeb DeveloperAuthor Commented:
It works
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

From novice to tech pro — start learning today.