- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP exit()
- PHP exit() vs. break
- PHP isset()
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- PHP mysqli: Insert Record
- PHP mysqli: Update Record
- PHP mysqli: Fetch Record
- PHP mysqli: Delete Record
- PHP mysqli: SignUp Page
- PHP mysqli: LogIn Page
- PHP mysqli: Store User Data
- PHP mysqli Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP filemtime(): Get the Last Modified Time of a File
The PHP filemtime() function is used when we need to find the last time the file was modified. For example:
<?php echo "<p>The latest time when the file <b>codescracker.txt</b> was modified:</p>"; echo filemtime("codescracker.txt"); echo "<br>Or<br>"; echo date("d F, Y H:i:s", filemtime("codescracker.txt")); ?>
The output produced by the above PHP example using the filemtime() function is:
Since the filemtime() function returns the last modified time and date of the file in a Unix timestamp, that almost cannot be understood by a normal user. Therefore, let me use the date() function to convert the time into a format that a normal user can understand.
<?php $file = "codescracker.txt"; echo "<p>The file, $file last modified on:</p>"; $mt = filemtime($file); if($mt) { echo "Time: " .date("H:i:s", $mt); echo "<br>"; echo "Date: " .date("d F, Y", $mt); } else echo "Unable to find time, when the file last modified!"; ?>
Now the output produced by the above PHP example should be:
PHP filemtime() Syntax
The syntax of the filemtime() function in PHP is:
filemtime(filename)
The function filemtime() returns False on failure.
A frequent application of the filemtime() function is to determine whether a file has been modified since its last access. You can use this function, for instance, to determine if a cache file has expired and must be regenerated.
Advantages of the filemtime() function in PHP
- The ability to retrieve a file's last modified time is its main benefit. This feature can be helpful in a variety of circumstances where you need to know whether a file has been updated since the last time you accessed or processed it.
- Because the entire file does not need to be read into memory, retrieving the file modification time with filemtime() is a comparatively quick operation.
- filemtime() is a built-in PHP function that can access file modification times on a variety of platforms and is supported by the majority of PHP-enabled servers.
Disadvantages of the filemtime() function in PHP
- The filemtime() function only returns the last modified time of a file; it does not return any other details, such as the file's size, owner, or permissions.
- If a file is modified more than once in the same second, filemtime() won't be able to distinguish between those modifications because the resolution of the timestamp it returns is limited to the second.
- If the modification time is not updated when checking for file modifications with filemtime(), the function may return the same timestamp even if the file contents have changed.
« Previous Tutorial Next Tutorial »