How to Retrieve a File’s Creation Time in PHP with filectime()

This article explains how to use PHP's filectime() function to obtain a file's creation timestamp, convert it to a readable date with date(), and handle missing files, while also mentioning related functions like filemtime() and fileatime() for comprehensive file time management.

php Courses
php Courses
php Courses
How to Retrieve a File’s Creation Time in PHP with filectime()

PHP provides the filectime() function to obtain a file’s creation time as a Unix timestamp.

The function returns the number of seconds since Jan 1 1970 UTC, which can be formatted with date(). Below is a complete example that checks for a file, retrieves its creation timestamp, formats it, and outputs the result.

<?php
$file = 'example.txt';
if (file_exists($file)) {
    $timestamp = filectime($file);
    $create_time = date('Y-m-d H:i:s', $timestamp);
    echo "File creation time is: $create_time";
} else {
    echo "File does not exist!";
}
?>

The script first defines the filename, uses file_exists() to verify its presence, then calls filectime() to get the timestamp, converts it with date(), and finally echoes the formatted creation time.

If the file is missing, the else block handles the situation by printing a not‑found message.

Besides filectime(), PHP also offers filemtime() for modification time and fileatime() for access time, allowing flexible handling of file‑related timestamps in web applications.

In summary, filectime() is a practical function for retrieving a file’s creation time, helping developers improve efficiency and quality when working with file metadata.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

file timestampsfilectimephp file functions
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.