Home > Article > Backend Development > Use the PHP function "filemtime" to return the modification time of a file
PHP function "filemtime" can be used to get the last modification time of a file. Its use is very simple, just pass in the file path as a parameter, and the function will return a timestamp indicating the last modification time of the file. Next, I will introduce how to use this function and some code examples.
In PHP, we can use the "filemtime" function in the following way:
$file_path = 'path/to/file.txt'; // 文件路径 $modification_time = filemtime($file_path); // 获取文件的最后修改时间 echo "文件最后修改时间:" . date('Y-m-d H:i:s', $modification_time); // 将时间戳转换为可读格式
The above code first defines a file path variable $file_path
, you need to Replace it with the actual path of the file you want to get the modification time of. Then, we use the filemtime
function to pass in the file path parameter $file_path
to get the last modification timestamp of the file. Finally, use the date
function to convert the timestamp into a readable datetime format.
Code Example-1: Get the last modification time of the file
$file_path = 'path/to/file.txt'; $modification_time = filemtime($file_path); echo "文件最后修改时间:" . date('Y-m-d H:i:s', $modification_time);
In the above example, we assume that the path of the file is 'path/to/file.txt'
. You can change the file path according to the actual situation and output the last modification time in different date and time formats through the date
function.
Code Example-2: Get the last modification time of multiple files
$files = array( 'path/to/file1.txt', 'path/to/file2.txt', 'path/to/file3.txt' ); foreach ($files as $file_path) { $modification_time = filemtime($file_path); echo "文件:'" . basename($file_path) . "' 最后修改时间:" . date('Y-m-d H:i:s', $modification_time) . "<br>"; }
In the above example, we defined an array containing multiple file paths$files
. By looping through the array, we get the last modification time of each file one by one, and use the basename
function to get the file name (without the path), and finally use the date
function to convert the timestamp to Readable format and output.
Summary:
This article introduces how to use PHP's "filemtime" function to get the last modification time of a file. By passing in the file path as a parameter, the function will return a timestamp indicating the last modified time. Timestamps can be converted into a readable format using the date
function. Through code examples, we show ways to get the last modification time of a single file and multiple files. I hope this article can help readers better understand and use the "filemtime" function.
The above is the detailed content of Use the PHP function "filemtime" to return the modification time of a file. For more information, please follow other related articles on the PHP Chinese website!