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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

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

The time and date can be modified freely in the watermark camera. Some users are not sure how to modify the time and date in the watermark camera. You can click on the watermark in the shooting interface to modify it. Next, the editor brings users a method to modify the time and date. Introduction, interested users come and take a look! Scanner Almighty King's tutorial on how to modify the time and date with watermark camera Answer: You can click on the watermark in the shooting interface to modify the details. Introduction: 1. Open the [Watermark Camera] software. 2. Click the time watermark on the screen. 3. After freely editing the time, click [√] at the back. 4. Click the date watermark to modify it. 5. After completing the modification, you can take pictures.

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

Under certain circumstances, strictly restricting users from making any adjustments to time can ensure that the various data generated during the entire system operation remain accurate and inherently stable and consistent. However, although many users are not clear on how to implement this time limit on Windows 10 Home Edition computers, you can still refer to the following detailed steps to implement this specification requirement. Win10 Home Edition prohibits time modification. Method 1. Press win+ RThen select "Run" 2. Enter "secpol.msc" 3. Click "Local Policy" - "User Rights Assignment" 4. Double-click on the right to open "Change System Time" 5. Select the user name, click Delete, click Apply and Confirm to complete the setting

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 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 returns the last modification timestamp of a file. The timestamp is one from the UNIX epoch January 1970

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

The 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';//File path

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

Title: Tips for using Golang to modify the system time. In the daily development process, sometimes we need to modify the system time for some testing or debugging, and this function can be easily achieved using the Golang programming language. This article will introduce how to modify the system time in Golang and provide specific code examples. First, we need to import the time package, which is the standard library for handling time in Golang. Then, we can use the time.Now() function to get the current time, through ti

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

1. First open the Tencent Meeting app and click on the scheduled meeting. 2. Then click the three dots in the upper right corner, and then click Modify meeting information. 3. Then click Modify Time, select the time you want to set, and click OK.

PHP file operation function example: directory traversal PHP file operation function example: directory traversal Jun 21, 2023 am 10:04 AM

PHP is a very popular programming language that is widely used for web development, especially server-side development. File operation is an essential part of Web development. PHP provides a wealth of file operation functions. This article will introduce one of them: directory traversal. Directory traversal refers to traversing directories in the file system and obtaining files and subdirectories in the directories. In web development, directory traversal is often used for functions such as site map creation and file resource management, and it can also be used for website vulnerability detection and other aspects. Below, we will use examples to learn P

Detailed explanation of PHP file functions: realizing file reading, writing and operating functions Detailed explanation of PHP file functions: realizing file reading, writing and operating functions Nov 20, 2023 pm 01:17 PM

PHP is a high-performance scripting language widely used for web development. In PHP, file operation is a very common and important function. This article will introduce in detail the use of file functions in PHP to help readers realize the reading, writing and operating functions of files. 1. Opening and closing files In PHP, the fopen function is used to open files. The syntax is as follows: $file=fopen("file path", "open mode"); where, file path

See all articles