Example 1 — Show When a File Changed
Create a file and print the time it was last modified in a readable format.
<?php
$file = __DIR__ . '/mtime_demo.txt';
file_put_contents($file, 'Hello');
$time = filemtime($file);
echo 'Last modified: ', date('Y-m-d H:i:s', $time), PHP_EOL;
unlink($file); How It Works
- file_put_contents() writes the file, which sets its modification time to now.
- filemtime() returns that time as a Unix timestamp.
- date() converts the timestamp into a year-month-day hour:minute:second format.
- The time you see will match the moment you run the script.

