/    Sign up×
Community /Pin to ProfileBookmark

Last Modified Include

Hi. I am hoping someone can point me in the right direction:

I using php to print last modified date on each of my pages. On each page, I have:
[FONT=Courier New]
<php? $last_modified = filemtime(“FILENAME.php”); print “Last modified ” . date(“l, dS F, Y “, $last_modified); ?>[/FONT]

I’d rather use an include here, so that if I want to change the format, I can do it in one place. In the include file, I guess I would need to create a variable inside filemtime() that represented the php file calling the include. Is this possible? If so, how? Thanks in advance.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@aaronbdavisMay 15.2006 — I would suggest modifying it slightly (this is untested, so you may have to tweak it a bit):[code=php]$last_modified = filemtime($_SERVER['PHP_SELF']);
print "Last modified " . date("l, dS F, Y ", $last_modified);[/code]
Then you ought to be able to include this into an Include file.
Copy linkTweet thisAlerts:
@ShmohelMay 15.2006 — Save as [b]filetime.php[/b]
[code=php]<?
$last_modified = filemtime($_GET['file']);
echo "Last modified " . date("l, dS F, Y ", $last_modified);
?>[/code]


Then, where you want it to display:
[code=php]
require('filetime.php?file=filename.php');
[/code]
Copy linkTweet thisAlerts:
@askohenauthorMay 15.2006 — Hmm. I can't get either of these to work. Shmohel, I am wondering about the 'file' parameter in the $_GET. Doesn't this need to be a variable that represents the file where the include is embedded?
Copy linkTweet thisAlerts:
@ShmohelMay 15.2006 — Yea, if all these files are not in the same folder, it will not work. What does the file structure you are going for look like? Where is [b]filename.php[/b] located?
Copy linkTweet thisAlerts:
@bokehMay 15.2006 — I using php to print last modified date on each of my pages. [/QUOTE]What's the purpose of doing that?
Copy linkTweet thisAlerts:
@WebnerdMay 15.2006 — try just this
[code=php]
<?
print "Last modified " . date("l, dS F, Y ", filemtime(__FILE__));
?>
[/code]

.

__FILE__ is a PHP constant for the current file. Using $_SERVER["PHP_SELF"] won't always work as anticipated as it pulls the resulting PHP file name and not the served file name
Copy linkTweet thisAlerts:
@askohenauthorMay 16.2006 — try just this
[code=php]
<?
print "Last modified " . date("l, dS F, Y ", filemtime(__FILE__));
?>
[/code]

[/QUOTE]


Ok. I tried the above two examples, both of which appear not to work. My directory structure for now is simple. My all my php files including includes are in the same directory for now. As a test of the last suggested script, I added the following arguments to the date() function, so that the include now reads:

<?php

print "Last modified " . date("l, dS F, Y, G i", filemtime(__FILE__));

?>

This way I can check the time and test the script. It appears that the script offered by webnerd is returning the last modified date of the include file, not the file that is calling the include.
Copy linkTweet thisAlerts:
@WebnerdMay 16.2006 — That is correct, it returns the actual date of the include file. If you want the date of the processing file, you would use:

filemtime(realpath($_SERVER["PHP_SELF"]))
Copy linkTweet thisAlerts:
@NogDogMay 16.2006 — Include file (mtime.php):
[code=php]
<?php
function last_mod($file)
{
printf("<p id='last_mod'>Last modified: %s</p>n",
date("l, dS F, Y, G:i:s", filemtime($file)));
}
?>
[/code]

Main file:
[code=php]
<?php if(include "mtime.php") { last_mod(__FILE__); } ?>
[/code]
×

Success!

Help @askohen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...