279 of 410 menu

The filemtime Function

The filemtime function returns the Unix timestamp of the last modification time of a file. The file path is passed as a parameter. If the file does not exist, the function returns false.

Syntax

filemtime(string $filename): int|false

Example

Get the modification time of the current file:

<?php $res = filemtime(__FILE__); echo $res; ?>

Code execution result:

1672531200

Example

Check if the file exists and output the modification date:

<?php $file = 'test.txt'; if (file_exists($file)) { echo 'Last modified: ' . date('Y-m-d H:i:s', filemtime($file)); } else { echo 'File not found'; } ?>

Code execution result:

'Last modified: 2023-01-01 12:00:00'

Example

Compare the modification time of two files:

<?php $file1 = 'file1.txt'; $file2 = 'file2.txt'; if (filemtime($file1) > filemtime($file2)) { echo 'File1 is newer'; } else { echo 'File2 is newer or equal'; } ?>

Code execution result:

'File1 is newer'

See Also

  • the filectime function,
    which returns the creation time
  • the fileatime function,
    which returns the access time
  • the touch function,
    which changes the modification time
byenru