0

How may I get the saved date from the body (problem.php) file displayed in the footer.php portion of the created HTML file?

<?php
// problem.php
include_once 'top.php'; 
// Top.php provides <!DOCTYPE html> <HTML LANG="en"> <head> <title></title> </head> <body>
?>

<h2>Show the date this was saved in the footer</h2>

<p> Using XAMPP 8.1.6  HTTP/1.1" 200 and PHP 5.2.0 I try to learn.</p> 

<p> My search have shown that document.write is bad. The only thing I have been able to make work is the script below. This will correctly show YESTERDAY's date here. The same script in the footer shows today's date.</p>

<p> How may I get this into a variable and use that in the FOOTER?</p>

<script language="javascript">
months = ['January', 'Febraury', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var theDate = new Date(document.lastModified);
theDate.setTime((theDate.getTime()+(5000*60*60)) )
with (theDate) {
document.write("<i>Last updated "+getDate()+' '+months[getMonth()]+' '+(getYear()+1900)+"</i>")
}
</script>

<?php
include_once 'footer.php'; 
// Footer.php provides </body> </html>
?> 

Edit: PHP dynamically creates the page so the date I see is the date the page was created (today). I wish to display the date the main body of the page (problem.php) was last changed or uploaded.

Edit 2: imvain2 provided the perfect answer with <?php echo date ("F d Y", filemtime(basename($_SERVER['SCRIPT_FILENAME']))); ?> in footer.php. That worked when I changed my machine's date.

FKlusmann
  • 11
  • 5
  • Does this answer your question? [Displaying last modified date in PHP](https://stackoverflow.com/questions/31437691/displaying-last-modified-date-in-php) – imvain2 Aug 11 '22 at 19:55
  • You can just get the last modified date of the file via PHP – imvain2 Aug 11 '22 at 19:56
  • Does this answer your question? [How to pass arguments to an included file?](https://stackoverflow.com/questions/4315271/how-to-pass-arguments-to-an-included-file) – Ashwin Aug 11 '22 at 19:56
  • Thanks all. @imvain2, From this, how can I get the date problem.php was saved into a variable in the file? – FKlusmann Aug 11 '22 at 20:19
  • @Ashwin, thank you. My major problem is in creating the variable in the first place. – FKlusmann Aug 11 '22 at 20:20
  • @FKlusmann can you elaborate? – Ashwin Aug 11 '22 at 20:21
  • @Ashwin, PHP dynamically creates the HTML. The date I see is the date the page was created (today). I wish to display the date the main body PHP was saved. – FKlusmann Aug 11 '22 at 20:48

2 Answers2

1

In footer.php you can echo the date modified of the current script file name. This will get the date modified of every page without passing in any specific file names

Date Converts to a date format

filemtime Gets the modified date of the file passed to it

basename Strips the file name from the path passed to it

$_SERVER['SCRIPT_FILENAME'] is the name of the file that is being processed and sent to the browser.

echo date ("F d Y H:i:s", filemtime(basename($_SERVER['SCRIPT_FILENAME'])))
imvain2
  • 15,480
  • 1
  • 16
  • 21
  • in the same directory I have "quote.php", saved 8/9/2022. I tried " and get "Warning: Undefined array key "quote.php" in....." The same with the full path, How do I get the file name in the array? Thanks. – FKlusmann Aug 11 '22 at 21:31
  • @FKlusmann if you are adding footer.php to quote.php and problem.php, just use my code as is. It grabs the current page and gets that page's date modified. You don't have to change what is being passed to it – imvain2 Aug 11 '22 at 21:51
  • I have verified that quote.php is sent as one of the includes in my bigger test. – FKlusmann Aug 11 '22 at 21:51
  • THANK YOU! I changed my computer's date and ran with your code in footer.php! IT WORKED. THANK YOU! – FKlusmann Aug 11 '22 at 22:05
  • I lack the reputation to upvote your answer. :( – FKlusmann Aug 11 '22 at 22:07
  • I read somewhere: "note that PHP caches the return value of filemtime() internally, thus clearstatcache() should be called before." - not sure if this is relevant. Also filemtime seems to be DST sensitive? – mplungjan Aug 12 '22 at 05:53
0

You posted and tagged JavaScript so here is a JS solution

I post this for people coming after.

NOTE document.lastModified will always be today since the page is dynamically created and the browser does not know the date of the server file problem.php unless you pass the server creation date to the script instead. You could change

let theDate = new Date(document.lastModified);

to

let theDate = new Date(<?= filemtime(basename($_SERVER['SCRIPT_FILENAME'])) ?>*1000);

That said,

Assuming you have a

<div class="footer">
</div>

in your footer you can do this but please use getFullYear() instead of adding 1900 to getYear and with is not recommended.

const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
let theDate = new Date(document.lastModified);
theDate.setTime(theDate.getTime() + (5000 * 60 * 60));
window.addEventListener("DOMContentLoaded", function() {
  document.querySelector(".footer").innerHTML += `<hr/><i>Last updated ${theDate.getDate()} ${months[theDate.getMonth()]} ${theDate.getFullYear()}</i>`;
})
<div class="footer"></div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Thank you @mplungjan. "NOTE: document.lastModified will be today since the page is dynamically created" is exactly what I am trying to eliminate. – FKlusmann Aug 11 '22 at 20:15
  • will "window.addEventListener("DOMContentLoaded", function()" work on a machine not running M$ Windows? – FKlusmann Aug 11 '22 at 20:31
  • It will work on any normal browser since 2011: https://caniuse.com/domcontentloaded – mplungjan Aug 12 '22 at 05:41
  • @FKlusmann I gave you a php/JS hybrid now so you can insert the date anywhere from problem.php to another template at runtime – mplungjan Aug 12 '22 at 06:00