What is the filemtime() Function?

The filemtime() function is a built-in PHP function that returns the last modification time of a file. This function returns a Unix timestamp representing the time the file was last modified.

Here's the basic syntax of the filemtime() function:

filemtime(filename);

Where filename is the name of the file to be checked.

How to Use the filemtime() Function?

Using the filemtime() function is straightforward. Here are the steps to follow:

  1. Call the filemtime() function, passing in the name of the file you want to check.
  2. The function will return a Unix timestamp representing the time the file was last modified.
  3. You can format the Unix timestamp using the date() function to display the time in a more human-readable format.

Here's an example code snippet that demonstrates how to use the filemtime() function:

<?php

$filename = 'myfile.txt';
$last_modified_time = filemtime($filename);
$modified_time_string = date('F d Y H:i:s.', $last_modified_time);
echo "The file $filename was last modified on $modified_time_string";

In this example, we check when the file myfile.txt was last modified using the filemtime() function. We store the Unix timestamp representing the last modification time in the $last_modified_time variable. We then format the Unix timestamp using the date() function and store it in the $modified_time_string variable. Finally, we output a message indicating when the file was last modified, using the formatted time string.

Conclusion

The filemtime() function is a useful tool in PHP for checking when a file was last modified. By following the steps outlined in this guide, you can easily use the filemtime() function in your PHP projects to check when files were last modified.

Practice Your Knowledge

What does the filemtime() function in PHP do?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?