How to determine file age using PHP?

Php

Php Problem Overview


Any way to determine file age of an image in a folder using PHP?

I want to delete older files than 2 hours, is this possible without adding timestamp-names to their filenames on upload to the folder?

If so, please give me an example!

thanks

Php Solutions


Solution 1 - Php

if (time()-filemtime($filename) > 2 * 3600) {
  // file older than 2 hours
} else {
  // file younger than 2 hours
}

Solution 2 - Php

You can use filemtime function to get the last modified date/time and use that to see how old the file is.

Solution 3 - Php

You are looking for the filemtime function

<?php
// outputs e.g.  somefile.txt was last modified: December 29 2002 22:16:23.

$filename = 'somefile.txt';
if (file_exists($filename)) {
    echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}
?>

Solution 4 - Php

I digged a bit on the web and Stackoverflow (https://stackoverflow.com/questions/2915864/php-how-to-find-the-time-elapsed-since-a-date-time) and here is my solution:

It's a full answer giving a human readable representation of file age + a check to see if the file is 2 hours old or not.

<?php

function humanTiming ($time)
{
	// to get the time since that moment
    $time = time() - $time;

	// time unit constants
    $timeUnits = array (
        31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
    );

	// iterate over time contants to build a human 
	$humanTiming;
    foreach ($timeUnits as $unit => $text)
	{
        if ($time < $unit)
			continue;
        $numberOfUnits = floor($time / $unit);

		// human readable token for current time unit
		$humanTiming = $humanTiming.' '.$numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
		
		// compute remaining time for next loop iteration
		$time -= $unit*$numberOfUnits;
    }
    return $humanTiming;
}


$filename = '/path/to/your/file.extension';

if (file_exists($filename))
{
	echo 'Now = '.date ("Y-m-d H:i:s.", time());
    echo "<br/>$filename was last modified on " . date ("Y-m-d H:i:s.", filemtime($filename));
	$time = strtotime('2010-04-28 17:25:43');
	$time = filemtime($filename);
	echo '<br/>File age is '.humanTiming($time).'.';
	
	$fileAge = time()-filemtime($filename);
	
	echo '<br/>Is file older than 2 hours? '.(($fileAge < (2*3600))?'No':'Yes').'.';
}
?>

Here is the output on my side:

Now = 2013-01-29 09:10:12.
/path/to/your/file.extension was last modified on 2013-01-29 08:07:52.
File age is 1 hour 2 minutes 20 seconds.
Is file older than 2 hours? No.

Solution 5 - Php

Be aware that on NAS/SAN storages the file time of an up-to-date file might be different from the PHP time(), as those storages might have their own clock. I think such configurations are rare, but it cost me some headache.

Solution 6 - Php

Try with filemtime()

> This function returns the time when the data blocks of a file were being written to, that is, the time when the content of the file was changed. > > int filemtime ( string $filename ) > > * filename: Path to the file.

// outputs e.g.  somefile.txt was last modified: December 29 2002 22:16:23.

$filename = 'somefile.txt';
if (file_exists($filename)) {
    echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}

Solution 7 - Php

You can use date/time of last file modification:

Solution 8 - Php

I'd check out the filemtime() or the filectime() functions. Those will give you the modified and creation times (respectively) of the file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser188962View Question on Stackoverflow
Solution 1 - PhpannebView Answer on Stackoverflow
Solution 2 - PhpMarek KarbarzView Answer on Stackoverflow
Solution 3 - PhpRochView Answer on Stackoverflow
Solution 4 - PhpPascalView Answer on Stackoverflow
Solution 5 - PhpGerfriedView Answer on Stackoverflow
Solution 6 - PhpGabriel SosaView Answer on Stackoverflow
Solution 7 - PhpKarolView Answer on Stackoverflow
Solution 8 - PhpDave DeLongView Answer on Stackoverflow