Return to Snippet

Revision: 11641
at February 12, 2009 09:07 by fackz


Updated Code
function Sort_Directory_Files_By_Last_Modified($dir, $sort_type = 'descending', $date_format = "F d Y H:i:s.")
{
$files = scandir($dir);

$array = array();

foreach($files as $file)
{
	if($file != '.' && $file != '..')
	{
		$now = time();
		$last_modified = filemtime($dir.$file);

		$time_passed_array = array();

		$diff = $now - $last_modified;

		$days = floor($diff / (3600 * 24));

		if($days)
		{
		$time_passed_array['days'] = $days;
		}

		$diff = $diff - ($days * 3600 * 24);

		$hours = floor($diff / 3600);

		if($hours)
		{
		$time_passed_array['hours'] = $hours;
		}

		$diff = $diff - (3600 * $hours);

		$minutes = floor($diff / 60);

		if($minutes)
		{
		$time_passed_array['minutes'] = $minutes;
		}

		$seconds = $diff - ($minutes * 60);

		$time_passed_array['seconds'] = $seconds;

	$array[] = array('file'         => $file,
		             'timestamp'    => $last_modified,
		             'date'         => date ($date_format, $last_modified),
		             'time_passed'  => $time_passed_array);
	}
}

usort($array, create_function('$a, $b', 'return strcmp($a["timestamp"], $b["timestamp"]);'));

if($sort_type == 'descending')
{
krsort($array);
}

return array($array, $sort_type);
}

//Example of usage:

$dir  = '/home/public_html/my_directory/';

$array = Sort_Directory_Files_By_Last_Modified($dir);

// Info Array
$info = $array[0];

// Sort Type
$sort_type = $array[1];

echo '<h3>'.$dir.'</h3>';

echo 'Order by: Last Modified ('.$sort_type.')<br>';

foreach($info as $key => $detail)
{
	echo '<h4>'.$detail['file'].'</h4>';

	echo 'Last Modified: '.$detail['date'].'<br>';

	$time_passed = '';

	foreach($detail['time_passed'] as $type => $value)
	{
	$time_passed .= $value." ".$type.", ";
	}

	$time_passed = "<span>".rtrim($time_passed, ", ")."</span> ago";

	echo $time_passed."nn";

}

Revision: 11640
at February 12, 2009 09:06 by fackz


Initial Code
function Sort_Directory_Files_By_Last_Modified($dir, $sort_type = 'descending', $date_format = "F d Y H:i:s.")
{
$files = scandir($dir);

$array = array();

foreach($files as $file)
{
	if($file != '.' && $file != '..')
	{
		$now = time();
		$last_modified = filemtime($dir.$file);

		$time_passed_array = array();

		$diff = $now - $last_modified;

		$days = floor($diff / (3600 * 24));

		if($days)
		{
		$time_passed_array['days'] = $days;
		}

		$diff = $diff - ($days * 3600 * 24);

		$hours = floor($diff / 3600);

		if($hours)
		{
		$time_passed_array['hours'] = $hours;
		}

		$diff = $diff - (3600 * $hours);

		$minutes = floor($diff / 60);

		if($minutes)
		{
		$time_passed_array['minutes'] = $minutes;
		}

		$seconds = $diff - ($minutes * 60);

		$time_passed_array['seconds'] = $seconds;

	$array[] = array('file'         => $file,
		             'timestamp'    => $last_modified,
		             'date'         => date ($date_format, $last_modified),
		             'time_passed'  => $time_passed_array);
	}
}

usort($array, create_function('$a, $b', 'return strcmp($a["timestamp"], $b["timestamp"]);'));

if($sort_type == 'descending')
{
krsort($array);
}

return array($array, $sort_type);
}

Initial URL
http://www.bitrepository.com/web-programming/php/sort-files-by-filemtime.html

Initial Description
This is a function that selects files from a directory and orders them by the last time they were changed, in ascending or descending order. The snippet also calculates how much time passed since the file’s content was changed.

Initial Title
Sort files from directory & order them by filemtime()

Initial Tags
sort, files, directory

Initial Language
PHP