Home  >  Article  >  Backend Development  >  How to get the time attribute (information) of the file in php file operation

How to get the time attribute (information) of the file in php file operation

青灯夜游
青灯夜游Original
2021-08-04 18:24:442911browse

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:

How to get the time attribute (information) of the file in php file operation

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:

How to get the time attribute (information) of the file in php file operation

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(&#39;Y-m-d H:i:s&#39;,filectime($file));
echo "<br>文件修改时间为:".date(&#39;Y-m-d H:i:s&#39;,filemtime($file));
echo "<br>文件上次访问的时间为:".date(&#39;Y-m-d H:i:s&#39;,fileatime($file));
?>

Output result:

How to get the time attribute (information) of the file in php file operation

Okay, that’s all. If you want to know anything else, you can click this. → →

Basic Operations of PHP Files

Finally, I would like to recommend to you the latest free course on our platform "

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!

Statement:
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