


PHP file operation function example: file last modification time
Jun 21, 2023 am 11:43 AMPHP is a widely used server-side programming language. It has powerful file operation capabilities, and the final modification time of a file is also a common requirement for file operations. Therefore, in this article, we will explore an example of PHP file operation function-how to get the last modification time of a file.
- Using the filemtime() function
PHP provides a built-in function called filemtime(), which is used to return the last modification timestamp of a file. The timestamp is the number of seconds since 00:00:00, January 1, 1970, UNIX epoch time, so it is an integer value.
The following is a sample code that uses the filemtime() function to obtain the last modification time of a file:
$filename = 'example.txt'; if (file_exists($filename)) { echo "文件最后修改时间:" . date("Y-m-d H:i:s.", filemtime($filename)); } else { echo "文件不存在"; }
In the above code, first determine whether the specified file exists. If it exists, use the filemtime() function to obtain the last modification time of the file, and use the date() function to format the output time. In the output string, use dot notation to remove the decimal part of the timestamp so that it becomes an integer.
- Use the stat() function
Another way to get the last modification time of a file is to use the stat() function. The function returns an array containing details about the file, including file type, modification time, access time, etc.
The following is an example code for using the stat() function to obtain the last modification time of a file:
$filename = 'example.txt'; if (file_exists($filename)) { $stat = stat($filename); echo "文件最后修改时间:" . date("Y-m-d H:i:s", $stat['mtime']); } else { echo "文件不存在"; }
In the above code, first determine whether the specified file exists. If it exists, use the stat() function to obtain the file information array and obtain the last modification time from the array. Finally, use the date() function to format the output time.
- Conclusion
Through the introduction of this article, I believe you already understand how to get the last modification time of a file in PHP. This function can be implemented very conveniently using the built-in function filemtime() or stat(). It should be noted that before using these functions, you should first determine whether the corresponding file exists. If the file does not exist, the file information cannot be obtained and an exception will be thrown at runtime.
The above is the detailed content of PHP file operation function example: file last modification time. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to modify time and date on watermark camera

How to limit modification time in win10 home version

Use the PHP function 'filemtime' to return the modification time of a file

Tips for modifying system time using Golang

PHP file operation function example: file last modification time

How to modify the meeting time in Tencent Conference_The specific steps to modify the meeting time in Tencent Conference

How to write a simple file management system using PHP

PHP file operation function example: file deletion
