Home Backend Development PHP Tutorial PHP file operation function example: file last modification time

PHP file operation function example: file last modification time

Jun 21, 2023 am 11:43 AM
php file operations file function Change the time

PHP 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.

  1. 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 "文件不存在";
}
Copy after login

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.

  1. 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 "文件不存在";
}
Copy after login

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.

  1. 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to modify time and date on watermark camera How to modify time and date on watermark camera Mar 06, 2024 pm 12:40 PM

How to modify time and date on watermark camera

How to limit modification time in win10 home version How to limit modification time in win10 home version Jan 08, 2024 pm 08:34 PM

How to limit modification time in win10 home version

Use the PHP function 'filemtime' to return the modification time of a file Use the PHP function 'filemtime' to return the modification time of a file Jul 24, 2023 am 10:01 AM

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

Tips for modifying system time using Golang Tips for modifying system time using Golang Feb 28, 2024 pm 01:45 PM

Tips for modifying system time using Golang

PHP file operation function example: file last modification time PHP file operation function example: file last modification time Jun 21, 2023 am 11:43 AM

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 modify the meeting time in Tencent Conference_The specific steps to modify the meeting time in Tencent Conference Apr 02, 2024 pm 02:20 PM

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 How to write a simple file management system using PHP Sep 24, 2023 pm 02:04 PM

How to write a simple file management system using PHP

PHP file operation function example: file deletion PHP file operation function example: file deletion Jun 20, 2023 am 09:13 AM

PHP file operation function example: file deletion

See all articles