How to Insert a Time Stamp in PHP MySQL

Written by max power
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Insert a Time Stamp in PHP MySQL
Web developers use the time stamp to record a date-entry event. (Hemera Technologies/AbleStock.com/Getty Images)

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

Instructions

    Using MySQL

  1. 1

    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.

  2. 2

    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.

  3. 3

    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.

    Using PHP

  1. 1

    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.

  2. 2

    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 yourDatabaseTable SET timestamp = '$current_time' WHERE yourPrimaryKey ='$primary_key_variable' ";

    mysql_query($query) or die('The new time was not inserted');

  3. 3

    Upload the PHP file to your server.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.