Page 1 of 1

Display File with Closest Date -

Posted: Sun Nov 08, 2009 11:58 am
by adreck_php
Hello friends -

I have been playing with this for awhile and just can't seem to do exactly what I want, though maybe someone could give me a little perspective as I'm probably too zoomed in.

Basically, I have a snippet which opens a directory and lists all the files, I use the string from the name of the file (e.g. newsletter10012009.pdf > which becomes > newsletter10012009 to also get an accurate date for which the file (newsletter) was for, this is saved in a variable zdate. In another location on the site I want to open this directory and show only 1 file the closest string date - (not upload) to today. I'm just wondering the most efficient way to adapt this so....only november shows until december is uploaded.

please see the code below...

Code: Select all

 
function makedate($date){
global $month, $day, $year;
             $day = date("d");
               $month = date("m");
               $year = date("Y");
             }
 
// Load up an array with the directory listing: 
$dir = '/faithchurch/assets/files/Bulletin/'; 
echo '<ul>';
if ($handle = @opendir($dir)) { 
 while (false!== ($filename = readdir($handle))) { 
 $files[] = $filename; 
 } 
 closedir($handle); 
 natcasesort($files); 
 $files = array_reverse($files); 
 
 // Process the files from the directory listing; 
 // but skip any files beginning with dots (.): 
 foreach ($files as $filename) { 
 if (is_file($dir.$filename)) { 
 
$filetxt = substr($filename, 0, strrpos($filename, '.'));
 
$month = substr($filetxt, 9, 2);
$day = substr($filetxt, 11, 2);
$year = substr($filetxt, 13, 4);
 
$zdate = date("F j Y", mktime(0, 0, 0, $month, $day, $year));
 
 
echo '<li><a href="assets/files/Bulletin/'.$filename.'" target="_blank">'.$zdate.'</a></li>';
 } 
 } 
}
echo '</ul>';
 
As you can see this will yield:
all the documents in from today - backward

Thanks for any suggestions.

Re: Display File with Closest Date -

Posted: Mon Nov 09, 2009 2:48 am
by cpetercarter
You could reorder the date components in the names of your newsletter files, so that 'year' is first, then 'month', then 'day' eg 20091001 instead of 10012009. Then if you 'natcasesort' the list of files, and reverse the array order, the most recent file will be the first (ie it will be $files[0]). Does this help?

Re: Display File with Closest Date -

Posted: Mon Nov 09, 2009 10:54 am
by adreck_php
Thanks cpetercarter I tried your idea and it worked great...might play around with the essence of your idea using the specific array row in another way also...Thanks for your feedback.

Adreck_PHP