SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast jeffbax's Avatar
    Join Date
    Jun 2003
    Location
    New Jersey
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Working with PHP and Dates (and comparisons of file modified dates)

    I have a directory and I am using PHP to make a list of the files and links to them.

    What I want to do is list them in order of date, and only the top 5 recent files, but I'm not sure how to handle this information.

    How do you compare the dates of files?

    I know the loop to do the top 5 would be something like looping through, checking the dates, if the next file date is newer than the top of the array bump them all down one and add it to the top element... but I'm not sure about the php functions to do this

    Anyone have any good tutorials?

    Thanks,
    Jeff

    *edit*

    doh! found this
    http://www.sitepoint.com/forums/show...ight=php+dates

  2. #2
    SitePoint Enthusiast jeffbax's Avatar
    Join Date
    Jun 2003
    Location
    New Jersey
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well... can anyone help debug this then?
    Code:
    <html>
    <head>
    <title>PHP Browser</title>
    </head>
    <body>
    <?php
    // open current directory
    
    $location = './';
    
    //Open current directory
    $dp = opendir($location);
    $modified;
    $fileList[] = 1;
    
    //loop through directory
    while ($entry = readdir($dp)){
    	/*if $entry is a directory...
    	if (is_dir($location . $entry)){
    		echo '[Dir] ' . $entry . '<br />';
    	}*/
    	//if $entry is a file...
    	if (is_file($location . $entry)){
    	
    		//if this file has a newer date than file index 0
    		if(filemtime($entry) > filemtime($fileList[0])){
    			$fileList[4] = $fileList[3];
    			$fileList[3] = $fileList[2];
    			$fileList[2] = $fileList[1];
    			$fileList[1] = $fileList[0];
    			$fileList[0] = $entry;
    		}
    		
    		//if this fileList has a newer date than fileList index 1
    		else if(filemtime($entry) > filemtime($fileList[1])){
    			$fileList[4] = $fileList[3];
    			$fileList[3] = $fileList[2];
    			$fileList[2] = $fileList[1];
    			$fileList[1] = $entry;
    		}
    		
    		//if this fileList has a newer date than fileList index 2
    		else if(filemtime($entry) > filemtime($fileList[0])){
    			$fileList[4] = $fileList[3];
    			$fileList[3] = $fileList[2];
    			$fileList[2] = $entry;
    		}
    		
    		//if this fileList has a newer date than fileList index 3
    		else if(filemtime($entry) > filemtime($fileList[0])){
    			$fileList[4] = $fileList[3];
    			$fileList[3] = $entry;
    		}
    		
    		//if this fileList has a newer date than fileList index 4
    		else if(filemtime($entry) > filemtime($fileList[0])){
    			$fileList[4] = $entry;
    		}
    		
    	}
    }
    
    for (i = 0; i < 5; i++){
    	echo '[fileList] ' .  '<a href="' . $fileList[i] . '">' . $fileList[i] . '</a><br />';
    }
    
    //close directory
    closedir($dp);
    ?>
    </body>
    </html>
    updated

  3. #3
    SitePoint Enthusiast jeffbax's Avatar
    Join Date
    Jun 2003
    Location
    New Jersey
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aside from the arrays like... completely not working, I think that an issue is that the modified date is not the date when I copy the file into the directory, rather when it was first originally created.

    Would using the accessed date reset the list everytime somebody on the net tried to access the file?

  4. #4
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jeffbax
    Aside from the arrays like... completely not working, I think that an issue is that the modified date is not the date when I copy the file into the directory, rather when it was first originally created.

    Would using the accessed date reset the list everytime somebody on the net tried to access the file?
    I don't know what OS you're using, but if it's *nix, you can simply create the list of files in order of modified date:

    ls -lt

    Then just loop 5 times and you're done
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  5. #5
    SitePoint Enthusiast jeffbax's Avatar
    Join Date
    Jun 2003
    Location
    New Jersey
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well that is handy, OS X

  6. #6
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jeffbax
    Well that is handy, OS X
    ah, my favorite OS. Well, that should do the trick. I've often found that doing things direcly in unix is sometimes a lot easier than in php. I mean, not everything of course, just certain things.
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  7. #7
    SitePoint Zealot
    Join Date
    Feb 2005
    Posts
    195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well system/exec are expensive calls.

    Maybe you need to also see http://us2.php.net/manual/en/functio...rstatcache.php
    might help your orginal code to work.
    Set up a free Instant Arcade!!
    Find the domainname you want at ExpireWorks.com
    Free $6.95 directi account


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •