How to Insert a PHP Date & Timestamp in MySQL
- 1). 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.
- 2). 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.
- 3). 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(); - 4). 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. - 5). 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')");
Source...