PHP Function: Get a file’s last modified timestamp

I’ve recently found the need to show a file’s last modified timestamp. This function is useful when including CSS or JavaScript files. By including a query string will ensure web browsers re-download a fresh copy of the file rather than using a cached file.

Simply run this function as you’re including a file, such as:

The example above will add style.css’ last modified timestamp as a query string, therefore each time the style.css is updated; the timestamp will be updated forcing the web browser to download a new copy. You will need to include this function code for the above code to work properly:

You will have three options when using this function:

  • $filename = This is the filename of the item you wish to check. E.g. “style.css”.
  • $filepath = You can use this variable to set the filepath of the above filename. Such as “css/”.
  • $displayV = This option has a default value of “false”. You can change this variable to “true”, doing so will prepend “?v=” before the timestamp, so your final timestamp will look like: “?v=1438124400”.

The final result of code with look very similar to:

Comments

Christian Kumhofer
  • July 29th, 2018

I would like to put all files from the same folder that were created today (the same day) into a zip file. Older files not. How does the PHP script look like?

Ryan
  • July 29th, 2018

Hi Christian,

I’m not sure what you mean. This script will not automatically the current data file into a .zip file.

I guess you could create a script which would run via CRON at the end of each day to look into a folder and zip the files.

Unfortunately it’s not something I have done before so I cannot send any examples across.

Post a Comment