How can I display the last time the current document was updated in PHP?
+3
A:
filemtime(); // file modified time
Example from PHP.net,
<?php
// outputs e.g. somefile.txt was last modified: December 29 2002 22:16:23.
$file= 'somefile.txt';
if (file_exists($file)) {
echo "$file was last modified: " . date ("F d Y H:i:s.", filemtime($file));
}
?>
Jonathan Sampson
2009-08-27 18:17:43
+3
A:
If by "current document", you mean "script that is currently being executed", you can use something like this :
$timestamp = filemtime(__FILE__);
$date = date('Y-m-d H:i:s', $timestamp);
var_dump($date);
And the output is like :
string '2009-08-27 20:17:54' (length=19)
Which is the date and time it is right now in France ;-)
See filemtime
to get the last modification date of a file, and date and it's formatting options to convert the timestamp returned by filemtime
to something a human-being can understand.
Pascal MARTIN
2009-08-27 18:18:25
You're missing a ', $timestamp' in your second line of code.
christian studer
2009-08-27 18:21:22
Ergh! Indeed I did forgot that one (I've just corrected it) ; thanks for the comment!
Pascal MARTIN
2009-08-27 18:22:49
I tried this and the output I get is: string(19) "2009-08-27 18:39:01" What am I doing wrong? Sorry, new to PHP.
Nate Shoffner
2009-08-27 18:40:13
What time is it ? ^^ Is the date not correct, considering your file ?
Pascal MARTIN
2009-08-27 18:46:36
No, the time isn't correct. It's about 6 hours ahead. Also, what is the "string(19)" for?
Nate Shoffner
2009-08-27 18:54:08
the string(19) says $date is a string, 19 characters long ; var_dump is a function used to get debugging informations from variables -- you wouldn't use it to display the date on your real website, of course, when you are done developping. ;; 6 hours ahead is strange... maybe your php.ini contains a not-well-configured "date.timezone" directive ? Else, try setting the right timezone with http://php.net/manual/en/function.date-default-timezone-set.php at the beginning of your script
Pascal MARTIN
2009-08-27 19:11:25
Well, I got the time working for now. I don't necessarily need it, just the date. But the "string(19)" is still before the timestamp. Why is this?
Nate Shoffner
2009-08-28 05:44:39
OK about the date/time ;; about "string(19)", if you don't want that debugging information, don't use a debugging function like var_dump : use http://php.net/echo (the normal output construct) for instance.
Pascal MARTIN
2009-08-28 05:56:02
Thank you, sorry for being a noob haha.
Nate Shoffner
2009-08-28 06:28:49
no problem ^^ we all started one day ;-)Have fun !
Pascal MARTIN
2009-08-28 06:37:19