How to Insert a PHP Date & Timestamp in MySQL

Save

PHP works natively with the MySQL database, so these two languages are used in blogs, social networking sites and personal websites. When you create a MySQL database record, inserting the date and time stamp lets you keep track of when the record was created. You use the "date" function in PHP to retrieve the date, then insert it with other record information into your MySQL table.

  • Right-click the PHP file you want to use to insert the record. Select "Open With," then double-click the PHP editor in the list of programs.

  • Scroll down to the section of your code that contains the PHP code. All PHP code is contained within the opening "<?php" and closing "?>" tags.

  • Create a variable that contains the date. The "date" function in PHP lets you set up a date with any format. The format you use does not matter, because the MySQL database engine always saves the date with the default syntax. Type the following code to create a date variable:

    $date = date();

  • Create the database connection. Before you insert records into MySQL, you must set up a database connection. The following code creates a connection:

    $connection = mysql_connect("localhost","username","password");

    mysql_select_db("database_name", $connection);

    Replace "username" and "password" with your own MySQL database user name and password. Replace "database_name" with the name of the database on your server.

  • Create the "insert" query. For example, if you want to insert a new customer record, the following code creates a new record with the customer name "Joe Smith" and uses the data variable to insert the date into the table:

    mysql_query("insert into customer (FirstName, LastName, CreateDate)

    values ('Joe', 'Smith', $date')");

References

Promoted By Zergnet

Comments

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!