Page 1 of 1

Font color for key based on modif date ?

Posted: Mon Apr 13, 2009 10:35 pm
by Peuplarchie
Good day to you all,
here is a question about displaying the keys of an array of files and folders, with a different color based on the last modif date, this week, this year and older....

This week = # DB1212
This year = # 003300
Older = #000000


Here are my 2 question:
1) How and where can i add in my code an code which which would add to my array a file modification time ?

2) How to display in 3 categories, or more, of font color, the file and or folder based on the file modification time ?



I know of to find file modification time but, how and where could I put it in my code:

Code: Select all

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

Here is my code:

Code: Select all

 
 
<?php
 
$directory = "Art/";
function dirList ($directory)
{
 
    //create 2 arrays - one for folders and one for files
   $folders = array();
   $files = array();
 
    // create a handler for the directory
    $handler = opendir($directory);
 
    // keep going until all files in directory have been read
while (false !== ($file = readdir($handler))) {  
 
        // if $file isn't this directory or its parent,
        // add it to the results array
        if ($file != '.' && $file != '..')
        
        // If file is directory, mark it in bold.
       if(is_dir($directory.$file)) {
        array_push($folders,$file);
        
        // Else not styled
        }else{
        array_push($files,$file);
        
    }
    }
 
    echo "<ul id=\"".preg_replace('/\//','_',substr($directory,0,strlen($directory)-1))."\">\n"; //start a new unordered list for every iteration through dirList
 
$dircor = $directory;
    // tidy up: close the handler
    closedir($handler);
    foreach($folders as $folder) {
      echo "<li><div class=\"folder\"><b>".$folder."</b>\n<a href=\"javascript&#058;show('".preg_replace('/\//','_',$dircor."".$folder)."');\">Show</a>- <a href=\"javascript&#058;hide('".preg_replace('/\//','_',$dircor."".$folder)."');\">Hide</a></div>"; //echo the folder name enclosed in a list item
        dirList($directory.$folder.'/'); //loop through the contents of $folder
      echo "</li>\n"; //close this list item after all files and folders in $folder have been looped through
    
    
    }
 
    foreach($files as $file) {
    $filenamecor = substr($file, 0, -4);
      echo "<li><a href=\"index.html\" onclick=\"load('image_view.php?dir=".$dircor."&file=".$file."','boxdisp');return false;\">".$filenamecor."</a></li>\n"; //echo the file name enclosed in a list item
    }
 
 
    echo "</ul>\n"; //close the unordered list
}
 
dirList($directory);
 
?> 
 
 
 
Thnaks !

Re: Font color for key based on modif date ?

Posted: Tue Apr 14, 2009 3:48 pm
by silverspy18
Hi Peuplarchie, I played with the code you provided and got something working:

The following will echo the date the given file ($file) was last modified, colored accordingly using CSS:

Code: Select all

 
if (time() - filemtime($directory.$file) < 604800) {
    echo '<span style="color:#DB1212;">'.$file.' was last modified: ' . date ("F d Y H:i:s", filemtime($directory.$file)).'<span>';
} elseif (time() - filemtime($directory.$file) < 31556926) {
    echo '<span style="color:#003300;">'.$file.' was last modified: ' . date ("F d Y H:i:s", filemtime($directory.$file)).'<span>';
} else {echo '<span style="color:#000000;">'.$file.' was last modified: ' . date ("F d Y H:i:s", filemtime($directory.$file)).'<span>';}
 
Used within the context of your script I assigned a new array $file_modified to the string that was echoed above:

Code: Select all

 
<?php
 
$directory = "./";
function dirList ($directory)
{
 
    //create 2 arrays - one for folders and one for files
   $folders = array();
   $files = array();
 
    // create a handler for the directory
    $handler = opendir($directory);
 
    // keep going until all files in directory have been read
while (false !== ($file = readdir($handler))) {  
 
        // if $file isn't this directory or its parent,
        // add it to the results array
        if ($file != '.' && $file != '..')
       
        // If file is directory, mark it in bold.
       if(is_dir($directory.$file)) {
        array_push($folders,$file);
       
        // Else not styled
        }else{
        array_push($files,$file);
 
    if (time() - filemtime($directory.$file) < 604800) {
    $file_modified[] = '<span style="color:#DB1212;">'.$file.' was last modified: ' . date ("F d Y H:i:s", filemtime($directory.$file)).'<span>';
} elseif (time() - filemtime($directory.$file) < 31556926) {
    $file_modified[] = '<span style="color:#003300;">'.$file.' was last modified: ' . date ("F d Y H:i:s", filemtime($directory.$file)).'<span>';
} else {$file_modified[] = '<span style="color:#000000;">'.$file.' was last modified: ' . date ("F d Y H:i:s", filemtime($directory.$file)).'<span>';}
 
 
    }
    }
 
    echo "<ul id=\"".preg_replace('/\//','_',substr($directory,0,strlen($directory)-1))."\">\n"; //start a new unordered list for every iteration through dirList
 
$dircor = $directory;
    // tidy up: close the handler
    closedir($handler);
    foreach($folders as $folder) {
      echo "<li><div class=\"folder\"><b>".$folder."</b>\n<a href=\"javascript&#058;show('".preg_replace('/\//','_',$dircor."".$folder)."');\">Show</a>- <a href=\"javascript&#058;hide('".preg_replace('/\//','_',$dircor."".$folder)."');\">Hide</a></div>"; //echo the folder name enclosed in a list item
        dirList($directory.$folder.'/'); //loop through the contents of $folder
      echo "</li>\n"; //close this list item after all files and folders in $folder have been looped through
   
   
    }
 
    foreach($files as $key=>$file) {
    $filenamecor = substr($file, 0, -4);
      echo "<li><a href=\"index.html\" onclick=\"load('image_view.php?dir=".$dircor."&file=".$file."','boxdisp');return false;\">".$filenamecor."</a>&nbsp;".$file_modified[$key]."</li>\n"; //echo the file name enclosed in a list item
    }
 
 
    echo "</ul>\n"; //close the unordered list
}
 
dirList($directory);
 
?>
 
I also changed line 53 in order to be able to access the key of the element being accessed. Just move $file_modified[$key] around to where you want the string to be placed, right now it is placed right after the filename.

Hope this helps.

Re: Font color for key based on modif date ?

Posted: Wed Apr 15, 2009 10:41 pm
by Peuplarchie
Wow, exactly what I needed !
Thanks !
You are very good !
Take care !