Strip Section of File name (string) - format as F j Y
Posted: Sat Oct 24, 2009 8:17 pm
Hello friends -
I could use a little help. I am very close to a working script but I just need a few suggestion to refine it. Basically I have this snippet which opens a directory and lists all the files in the folder. That part works fine. Then I strip off the extension for the link text.
The files are named consistently as Bulletin_10192009.pdf
My client wants the name to be displayed as October 19 2009...so i separate each date node and then concatenate them again. I did this successfully with the code below, but it errors out if there is more than one file....because it can't call the function multiple times.
I could use a little help. I am very close to a working script but I just need a few suggestion to refine it. Basically I have this snippet which opens a directory and lists all the files in the folder. That part works fine. Then I strip off the extension for the link text.
The files are named consistently as Bulletin_10192009.pdf
My client wants the name to be displayed as October 19 2009...so i separate each date node and then concatenate them again. I did this successfully with the code below, but it errors out if there is more than one file....because it can't call the function multiple times.
Code: Select all
<?php
$dir = opendir('/assets/files/Bulletin/');
echo '<ul>';
while ($read = readdir($dir))
{
if ($read!='.' && $read!='..')
{
// Remove file extension //
$filename = substr($read, 0, strrpos($read, '.'));
// Strip-out Day nodes from String (filename) //
$month = substr($filename, 9, 2);
$day = substr($filename, 11, 2);
$year = substr($filename, 13, 4);
// format nodes as date //
function makedate($date){
global $month, $day, $year;
$day = date("d");
$month = date("m");
$year = date("Y");
}
//Format and join date elements //
$zdate = date("F j Y", mktime(0, 0, 0, $month, $day, $year);
// Print formatted date (zdate) as link text - set file location equal directory plus file name with extension //
echo '<li><a href="assets/files/Bulletin/'.$read.'" target="_blank">'.$zdate.'</a></li>';
}
}
echo '</ul>';
closedir($dir);
?>