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

青灯夜游
Release: 2023-03-11 21:56:01
Original
3005 people have browsed it

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);
?>
Copy after login

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));
?>
Copy after login
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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template