How can I get the time that the file was last modified?

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
APD1993
Forum Newbie
Posts: 10
Joined: Sat Mar 03, 2012 4:47 pm

How can I get the time that the file was last modified?

Post by APD1993 »

I am creating a series of scripts, one of which I would like to display the files in a particular directory. I think that I have managed to list the files through the use of a foreach loop, but I was wondering how I would be able to get the time that the file was last modified while using a foreach loop. However, I am getting an error message at the moment that reads:

Parse error: syntax error, unexpected ';' in C:\xampp\htdocs\viewfile3.php on line 13, but I do not see what the problem is :(

The coding for my script is below:

Code: Select all

<html><head>
<title>RCM File List</title>
<link rel="stylesheet" type="text/css" a href="/rcm/stylesheet.css">
</head>
<body>
<h1>Files in RCM Directory</h1>
<?php
$dir="C:/xampp/htdocs/rcm/*txt";
$filename='C:/xampp/htdocs/rcm/denman2.txt';
if (file_exists($filename)) {
foreach(glob($dir) as $file)
{
	$list = "<li><a href='readfile2.php'>Filename: $file</a></li><p>$filename was last changed on: " . date("F d Y H:i:s, filetime($filename));<br>";
}
}
?>

<ul>
<?php echo $list; ?>
</ul>
</body>
</html>
Any help is appreciated! :D
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: How can I get the time that the file was last modified?

Post by litebearer »

Try...

Code: Select all

$list = "<li><a href='readfile2.php'>Filename: " . $file , "</a></li><p>" . $filename . "was last changed on: " . date("F d Y H:i:s", filemtime($filename)) . "<br>";
APD1993
Forum Newbie
Posts: 10
Joined: Sat Mar 03, 2012 4:47 pm

Re: How can I get the time that the file was last modified?

Post by APD1993 »

When I tried that, I got the same error messages as before :(
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How can I get the time that the file was last modified?

Post by social_experiment »

Code: Select all

 $list = "<li><a href='readfile2.php'>Filename: $file</a></li><p>$filename was last changed on: " . date("F d Y H:i:s, filetime($filename));<br>";
 // should be
$list = "<li><a href='readfile2.php'>Filename: $file</a></li><p>$filename was last changed on: " . date("F d Y H:i:s, filetime($filename))<br>";
You have an extra ; in the code above; the second sample should solve the problem
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How can I get the time that the file was last modified?

Post by requinix »

social_experiment wrote:the second sample should solve the problem
It won't. Yours is missing a

Code: Select all

. "
right after the date() call. Meanwhile litebearer's was right but for the typoed comma instead of a period.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: How can I get the time that the file was last modified?

Post by litebearer »

Sorry about the typo :oops:

ALSO the function is filemtime NOT filetime.
APD1993
Forum Newbie
Posts: 10
Joined: Sat Mar 03, 2012 4:47 pm

Re: How can I get the time that the file was last modified?

Post by APD1993 »

litebearer wrote:Try...

Code: Select all

$list = "<li><a href='readfile2.php'>Filename: " . $file , "</a></li><p>" . $filename . "was last changed on: " . date("F d Y H:i:s", filemtime($filename)) . "<br>";
That seems to have worked :)

I was just wondering if there was a way that I could subtract 1 hour from the time being displayed as it is saying that I modified the file last at 13:14 when it was at 12:14 :)
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: How can I get the time that the file was last modified?

Post by litebearer »

Code: Select all

date("F d Y H:i:s", filemtime($filename)-3600)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How can I get the time that the file was last modified?

Post by requinix »

If you're correcting for the "wrong" timezone then you might want other dates corrected too. Use date_default_timezone_set.
APD1993
Forum Newbie
Posts: 10
Joined: Sat Mar 03, 2012 4:47 pm

Re: How can I get the time that the file was last modified?

Post by APD1993 »

Is there a way that I could implement this script so that if necessary, other text files in the RCM folder can be displayed? I added in a file called Test.txt and it is not being displayed in the script

Code: Select all

<!--Here, I am stating HTML coding with the <html> HTML tag. I am also creating a header and title (for the webpage) with the use of the <head> and <title> HTML tags. The title given to the webpage is RCM File List. I then am using the </title> and </head> HTML tags in order to stop the creation of the title and header of the webpage--> 
<html><head>
<title>RCM File List</title>
<!--Here, I am creating a header and title (for the webpage) with the use of the <head> and <title> HTML tags. The title given to the webpage is Login Details. I then am using the </title> and </head> HTML tags in order to stop the creation of the title and header of the webpage--> 
<link rel="stylesheet" type="text/css" a href="/rcm/stylesheet.css">
</head>
<!--Here, I am using the <body> HTML tag to create the body of the webpage so that content can be added to it-->
<body>
<!--Here, I am using the <h1> HTML tag to start creating a level 1 heading, and I am then using the </h1> HTML tag to stop creating the level 1 heading-->
<h1>Files in RCM Directory</h1>
<!--Here, I am starting a PHP script-->
<?php
//Here, I am creating a variable called dir and assigning it a value of a file path that helps act as a filter so that only files with the .txt extension such as the denman2.txt text file will be listed
$dir="C:/xampp/htdocs/rcm/*txt";
//Here, I am creating a variable called filename and I am assigning it a value of the filepath to the denman2.txt text file
$filename='C:/xampp/htdocs/rcm/denman2.txt';
//Here, I am creating an if statement branch stating that if the value assigned to the filename variable (i.e. the denman2.txt text file) exists, then go down this IF statement branch
if (file_exists($filename)) {
//Here, a foreach loop is being created while the files associated with the dir variable is being assigned as a variable called file
foreach(glob($dir) as $file)
{
//Here, I am creating a variable called list and I am assigning a formatted string that displays the name of the file associated with the file variable and then get the time that it was last modified so that it shows it in a Month day(number) year (number), hour:time:second format. I am also using "-3600" to set the time displayed back by an hour-->
$list = "<li><a href='readfile2.php'>Filename: " . $file . "</a></li><p>" . " Last Modified on: " . date("F d Y H:i:s", filemtime($filename)-3600) . "<br>";
}
}
//Here, I am ending the PHP script
?>
<!--Here, I am using the <ul> HTML tag to create an unordered list-->
<ul>
<!--I am starting a PHP script here-->
<!--I am using the echo keyword to write the value associated with the list variable to the webpage-->
<?php echo $list; ?>
<!--Here, I am using the </ul> HTML tag to end the creation of an unordered list-->
</ul>
<!--Here, I am ending the body and the adding of content to the webpage by using the </body> HTML tag and I am ending the HTML coding by using the </html> HTML tag-->
</body>
</html>
Thanks :)
Post Reply