In the previous article, we introduced the method of obtaining file attributes and checking whether the file is readable, writable, and executable. If you are interested, you can click the link to insert → "php file operation: check whether the file is executable. Read/Write/Execute》. Next, this article will continue to talk about file attributes and look at how to obtain file time attributes.
The file contains three time attributes (information), namely: Creation time, Modification time and Last access time.
For example, the following text file named "test.txt", its creation time, modification time and last access time are as follows:
So how do we get the three file attributes? Don’t panic, PHP provides three functions to get them:
filectime($filename)
: Returns the creation time of the file
filemtime($filename)
: Returns the last modification time of the file
fileatime($filename)
: Returns The last access time of the file
Let’s take a look at the following code example:
<?php header("Content-type:text/html;charset=utf-8"); $file = "test.txt"; echo "文件创建时间为:".filectime($file); echo "<br>文件修改时间为:".filemtime($file); echo "<br>文件上次访问的时间为:".fileatime($file); ?>
Output result:
By outputting the results, you will find that the three functions filectime(), filemtime() and fileatime() return time in the form of Unix timestamp, which is not conducive to reading, we can use ## The #date() function processes the obtained time and formats it into the specified "Y-m-d H:i:s" format.
<?php header("Content-type:text/html;charset=utf-8"); $file = "test.txt"; echo "文件创建时间为:".date('Y-m-d H:i:s',filectime($file)); echo "<br>文件修改时间为:".date('Y-m-d H:i:s',filemtime($file)); echo "<br>文件上次访问的时间为:".date('Y-m-d H:i:s',fileatime($file)); ?>
Entering the World of PHP from 0" ~ Come and learn!
The above is the detailed content of How to get the time attribute (information) of the file in php file operation. For more information, please follow other related articles on the PHP Chinese website!