How to Create a TimeStamp in PHP

A UNIX timestamp represents the number of seconds since the UNIX epoch date, which was the time at 00:00:00 UTC on 1 January 1970. The timestamp provides a unique number for each second that passes, and provides a common format to represent a date that many different types of computer systems understand. As a timestamp is an integer, it is easy to store in a database and makes it easier to perform date-based calculations, such as calculating the number of days between two dates. Use the PHP “mktime” command to create a UNIX timestamp.

1. Create a new HTML page in a web design application or text editor. Save the page as “timestamp.php.”

2. Add the following PHP code between the HTML “ ” tags:

This uses the PHP “time” command to output the timestamp at the moment the code is run. Save the page and upload it to your web server.

3. Open a Web browser and go to “timestamp.php”. You will see output similar to the following:

Current timestamp: 1305637455

1305637455 represents the number of seconds since the UNIX epoch date.

4. Change the PHP code to read:

This generates the timestamp for 12:01 12-25-2011. The parameters accepted by the mktime command are:

mktime($hour,$minute,$second,$month,$day,$year)

Save the file and upload it to your web server.

5. Refresh the “timestamp.php” page in your browser, and you will see the following displayed:

Timestamp: 1324771201

This shows the number of seconds between the UNIX epoch date and 12:01 12-25-2011.

Use the PHP “date” command to convert a timestamp back into a human-readable date. For example, try the following PHP code:

This will display as “12/25/2011 00:00:01” when you view it in a Web browser.

What is the unix time stamp?

The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970. Therefore, the unix time stamp is merely the number of seconds between a particular date and the Unix Epoch. This is very useful to computer systems for tracking and sorting dated information in dynamic and distributed applications both online and client side.

What happens on January 19, 2038?

On this date the Unix Time Stamp will cease to work due to a 32-bit overflow. Before this moment millions of applications will need to either adopt a new convention for time stamps or be migrated to 64-bit systems which will buy the time stamp a “bit” more time.