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.
Instructions
-
-
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 "<body> </body>" tags:
<?php echo "Current timestamp: " . time(); ?>
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:
<?php echo "Timestamp: " . mktime(0,0,1,12,25,2011); ?>
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.
-
1
Tips & Warnings
Use the PHP "date" command to convert a timestamp back into a human-readable date. For example, try the following PHP code:
<?php echo date("m/d/Y H:i:s",1324771201); ?>
This will display as "12/25/2011 00:00:01" when you view it in a Web browser.
References
Resources
- Photo Credit Jupiterimages/Photos.com/Getty Images