jperez260 Posted January 8, 2014 Share Posted January 8, 2014 (edited) Hello All, I'm trying to show the modication date of a file with php output. I added the "filemtime" to the print() function but I want it to show the date and time rather than just numbers. Any help would be greatly appreciated Here's my source: <style type="text/css"> * { font-family: tahoma; font-size: 16px; } </style> <?php // open this directory $myDirectory = opendir("."); // get each entry while($entryName = readdir($myDirectory)) { $pathinfo = pathinfo($entryName); if($pathinfo['extension'] != 'php, ink') $dirArray[] = $entryName; } // close directory closedir($myDirectory); // count elements in array $indexCount = count($dirArray); Print ("$indexCount files<br>\n"); // sort 'em sort($dirArray); // print 'em print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n"); print("<TR><TH><a style='float:left;' href='/joe'>Go Back Home</a></TH><TH></TH><TH></TH><TH></TH></TR>\n"); print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th><th>Modified</th></TR>\n"); // loop through the array of files and print them all for($index=0; $index < $indexCount; $index++) { if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>"); print("<td>"); print(filetype($dirArray[$index])); print("</td>"); print("<td>"); print(filesize($dirArray[$index])); print("</td>"); print("<td>"); print(filemtime($dirArray[$index])); print("</td>"); print("</TR>\n"); } } print("</TABLE>\n"); ?> Thank you all in advance Edited January 8, 2014 by jperez260 Quote Link to comment Share on other sites More sharing options...
requinix Posted January 8, 2014 Share Posted January 8, 2014 (edited) Try the date function: first argument is how you want the date formatted, second is the value from filemtime(). Also, if($pathinfo['extension'] != 'php, ink')The extension is either "php" or "inc". Right now you're checking if the extension is not "php, ink" (as in the file is script.php, ink). Edited January 8, 2014 by requinix Quote Link to comment Share on other sites More sharing options...
jperez260 Posted January 9, 2014 Author Share Posted January 9, 2014 Thank you...I forgot to remove the "ink". I tried the date function but I keep getting a php error // print 'em print("<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n"); print("<TR><TH><a style='float:left;' href='/joe'>Go Back Home</a></TH><TH></TH><TH></TH><TH></TH></TR>\n"); print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th><th>Modified</th></TR>\n"); // loop through the array of files and print them all for($index=0; $index < $indexCount; $index++) { if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files print("<TR><TD><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>"); print("<td>"); print(filetype($dirArray[$index])); print("</td>"); print("<td>"); print(filesize($dirArray[$index])); print("</td>"); print("<td>"); print(filemtime(date('F d Y H:i:s', $dirArray[$index]))); print("</td>"); print("</TR>\n"); } } print("</TABLE>\n"); This doesn't seem to work, I'm wondering if I need to delare it first some where or a variable. The problem is I'm already printing an array and not sure how to format the array of a file into date format. Anyother ideas? Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted January 9, 2014 Solution Share Posted January 9, 2014 You are using the functions filemtime() and date() backwards. You need to use the result of fimemtime() within the date function. Don't combine functions on one line untill you know them well. Also, don't use a for() loop for an array - use a foreach loop. Try this: // print 'em echo "<table border='1' cellpadding='5' cellspacing='0' class='whitelinks'>\n"; echo "<tr>\n"; echo " <th><a style='float:left;' href='/joe' colspan='4'>Go Back Home</a></th>\n"; echo "</tr>\n"; echo "<tr>\n"; echo " <th>Filename</th><th>Filetype</th><th>Filesize</th><th>Modified</th>\n"; echo "</tr>\n"; // loop through the array of files and print them all foreach($dirArray as $filePath) { if (substr($filePath, 0, 1) == ".") { // skip hidden files continue; } $fileType = filetype($filePath); $fileSize = filesize($filePath); $fileTime = date('F d Y H:i:s', filemtime($filePath)); echo "<tr>\n"; echo " <td><a href=\"{$filePath}\">{$filePath}</a></td>\n"; echo " <td>{$fileType}</td>\n"; echo " <td>$fileSize</td>\n"; echo " <td>{$fileTime}</td>\n"; echo "</tr>\n"; } echo "</table>\n"; Quote Link to comment Share on other sites More sharing options...
jperez260 Posted January 9, 2014 Author Share Posted January 9, 2014 This worked.. Thank you for your help. Very much appreciated Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.