How do I return the last modified date & time of a directory? This one returns the last modified time of the page/file: <?php $file = __FILE__; echo "This page was last modified on:" . date("F jS, Y @ g:i a", filemtime($file)); ?> PHP: But I need for it to output the last modified date & time of a certain folder (not the folder it's in, but a folder that I specify), how can I go about this? Thanks.
I'm not really asking for what method to use, just as long as it returns the last modified date & time of a directory (not a file). The code I posted uses the filemtime function.. I don't know how to return the last modified date of a directory, which is my problem.
It works on directories, but keep in mind, that modifying a file in the directory will not affect the time on the directory itself, if that's what you were looking to do. Also, don't add a trailing slash to the directory name.
LOL no, I'm just looking to output on a webpage the date & time a directory was last modified. A simple "The folder Blah was last update on: date & time". Displaying a file's last modified date is simple enough, but I don't know how to display a directory's last modified time, which is why I'm asking for assistance. Not looking to affect the time on the directory itself or anything idiotic like that.
Jestp is correct. But if you require assistance in getting this going here, try this, it will get the modified date and time of the current directory where this script is run: $abpath=$_SERVER[DOCUMENT_ROOT].strtr($_SERVER[PHP_SELF],array(basename($_SERVER[PHP_SELF])=>"")); $stat=stat($abpath); date("F jS, Y @ g:i a",$stat[mtime]); PHP: Hope this helps.
Wonderful , is there a way however to be able to specify the directory? Below is a snippet I got somewhere which is supposed to list down all files inside a directory, and include the last modified date for each file. I have messed it up enough that it now shows only the last modified date, it is now doing EXACTLY what I'm looking for, except that the script below lists all dates for EACH file inside the directory in an array: <?php function Sort_Directory_Files_By_Last_Modified($dir, $date_format = "F d Y H:i:s.") { $files = scandir($dir); foreach($files as $file) { if($file != '.' && $file != '..') { $now = time(); $last_modified = filemtime($dir); $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/site/public_html/Blah/Dir/'; $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 'Last updated: '.$detail['date'].'<br>'; } ?> PHP: Output on webpage: Now, it shows the last modified date of the specified directory, which is what I'm aiming for, but do you know how to simplify this by not going through the arrays, just returning the date of the last modified file once? Thanks for your help. I wanna be able to specify the directory, hope you can help some more?
Nevermind, I got it! <?php $abpath = '/home/site/public_html/folder/'; $stat=stat($abpath); echo date("F jS, Y @ g:i a",$stat[mtime]); ?> PHP: Thanks so much for showing me how it's done!
& Since I can't edit last post, just wanna add the filemtime version (just in case someone finds this thread through search engines, they'll be fed with more options): <?php $abpath = '/home/public_html/folder/'; echo date("F jS, Y @ g:i a", filemtime($abpath)); ?> PHP:
Thanks a lot for this thread. I never knew about the stat() function. I have an application where I can use this for caching. Thank you friends