Jump to content

Get Mod Date


jperez260

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

Link to comment
https://forums.phpfreaks.com/topic/285217-get-mod-date/
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).
Link to comment
https://forums.phpfreaks.com/topic/285217-get-mod-date/#findComment-1464501
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
https://forums.phpfreaks.com/topic/285217-get-mod-date/#findComment-1464509
Share on other sites

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
https://forums.phpfreaks.com/topic/285217-get-mod-date/#findComment-1464514
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.