PHP Web developers use PHP and MySQL to design websites and applications with dynamic data. One of the most important pieces of this data is the date and time. The date and time encodes into the database as the time stamp. MySQL databases are designed with a specific time stamp setting that allows developers to file and retrieve the date and time data in a standardised and predictable format.
- Skill level:
- Moderately Easy
Other People Are Reading
Create a new field in your database. Open the "phpMyAdmin" utility for your MySQL databases. Select the database of your choice from the left-hand menu. Select the table in which you wish to place the new time stamp field and click on the "Structure" action item. Hit the "Go" button next to the "Add One New Field" selection.
Set the attributes of the new field. Under the "Field" input box enter your new field's name. Name the field "Timestamp." Select the "Type" drop-down bar and tag the "Timestamp" options. This defines your new field as a "Time Stamp." Click on the "Default" drop-down bar. If desired, select the "Current_Timestamp" option. If selected, this option automatically creates a new time stamp whenever a new record is entered into your database. Now, click the "Attributes" drop-down box. Select the "On Update Current_Timestamp" menu item. If this option is selected, the time stamp field will update every time a change is made to your database record.
Save the changes. Select the "Save" option at the bottom right-hand corner of the window. The changes to your database structure are now saved. From this point on, the time stamp field automatically populates each time a new record is entered.
Capture the current time using the PHP date function and assign it to a PHP variable. The PHP date function takes the current time and formats it according to a set of values that you enter. Consider this example:
$current_time = date ("Y-m-d H:i:s");
In this line of code, it calls the date function and enters the values which format the output into the format of year, month, day, hour, minute and second or "2012-12-21 06:26:36." This is the exact time stamp format used by MySQL. In this example, the date is assigned to the current-time variable.
Insert the time stamp into the MySQL time stamp field of the database table of your choice. Use "PHP's mysql_query" function to complete this step. Here is the example.
$query = "UPDATE
timestamp= '$current_time' WHERE
mysql_query($query) or die('The new time was not inserted');
Upload the PHP file to your server.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for