1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Last modified time of a directory ?

Discussion in 'PHP' started by Love*, Aug 6, 2009.

  1. #1
    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. :)
     
    Solved! View solution.
    Love*, Aug 6, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    jestep, Aug 6, 2009 IP
  3. Love*

    Love* Well-Known Member

    Messages:
    1,739
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    165
    Digital Goods:
    1
    #3
    Love*, Aug 6, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    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.
     
    jestep, Aug 6, 2009 IP
  5. Love*

    Love* Well-Known Member

    Messages:
    1,739
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    165
    Digital Goods:
    1
    #5
    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. :)
     
    Love*, Aug 6, 2009 IP
  6. #6
    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. :D
     
    Leron, Aug 6, 2009 IP
  7. Love*

    Love* Well-Known Member

    Messages:
    1,739
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    165
    Digital Goods:
    1
    #7
    Wonderful :p, 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? :)
     
    Love*, Aug 6, 2009 IP
  8. Love*

    Love* Well-Known Member

    Messages:
    1,739
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    165
    Digital Goods:
    1
    #8
    Nevermind, I got it! :p

    <?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! :)
     
    Love*, Aug 6, 2009 IP
  9. Love*

    Love* Well-Known Member

    Messages:
    1,739
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    165
    Digital Goods:
    1
    #9
    & 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:
     
    Love*, Aug 7, 2009 IP
  10. Leron

    Leron Active Member

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    53
    #10
    Love...Glad you got it going :D
     
    Leron, Aug 8, 2009 IP
  11. HivelocityDD

    HivelocityDD Peon

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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
     
    HivelocityDD, Aug 9, 2009 IP