Timestamp in PHP

In this tutorial I will give you example how to add timestamp to file name in PHP .
This is useful when you create similar files like database backups, regular reports, or whatever you are saving in PHP with timestamp in the file name.

Bellow is an example that creates one CSV file with timestamp in the file name:

The part that sets the timestamp in PHP is:

The file created using this method will be like this: testFile.04-10-2011.csv – certainly with different date.
You can modify the PHP date function and use different formats. For example if you need only the current time for timestamp use:

The file name with this timestamp will be like: testFile.06:18:08.csv

For full timestamp including year-month-day-hour-minute-seconds change the code to:

My file appeared with timestamp: testFile.2011-10-04_06:18:08.csv

Certainly you can use different separators for the timestamp, also there are more options available which can be found at PHP date function page.


Filed Under: File ManagementPHPWEB Programing

Anthony Gee About the Author: Anthony G. is an IT specialist with more than 9 years of solid working experience in the Web Hosting industry. Currently works as server support administrator, involved in consultative discussions about Web Hosting and server administration. One of the first writers in the Onlinehowto.net website, now writing for Free Tutorials community - he is publishing tutorials and articles for the wide public, as well as specific technical solutions.

Comments (1)

  1. haritsinh says:

    your website is very useful and very clean and neat , keep doing what you do, thanks for sharing your knowledge.

Leave a Reply