search
HomeBackend DevelopmentPHP ProblemHow 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

Aug 04, 2021 pm 06:24 PM
phpChange the timecreation timedocumentfile properties

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.