Jump to content

Get Mod Date


jperez260
Go to solution Solved by Psycho,

Recommended Posts

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 by jperez260
Link to comment
Share on other sites

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 by requinix
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • Solution

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";
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.