Strip Section of File name (string) - format as F j Y

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adreck_php
Forum Newbie
Posts: 9
Joined: Sun Sep 07, 2008 6:01 pm

Strip Section of File name (string) - format as F j Y

Post by adreck_php »

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.

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);
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Strip Section of File name (string) - format as F j Y

Post by requinix »

adreck_php wrote:because it can't call the function multiple times.
Because it can't declare the function multiple times.

You put the

Code: Select all

function makedate($date){
inside the loop, which tells PHP to declare the function inside the loop. Each time. Of course, you can't define a function more than once, so PHP errors out.

Your code will be fine if you just move the makedate() declaration to some point before the loop.
(Actually, after the loop works too, but it makes more sense to do it before.)
adreck_php
Forum Newbie
Posts: 9
Joined: Sun Sep 07, 2008 6:01 pm

Re: Strip Section of File name (string) - format as F j Y

Post by adreck_php »

thanks for your help.....as soon as you said that I was like...darn....

clearly my head was too far zoomed see this obvious mistake...thanks...works great...
Post Reply