Home > Back-end >  Pass PHP HTML Date BODY Saved to the FOOTER
Pass PHP HTML Date BODY Saved to the FOOTER

Time:08-12

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.

CodePudding user response:

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'])))

CodePudding user response:

Assuming you have a

<div >
</div>

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

NOTE: document.lastModified will be today since the page is dynamically created

const months = ['January', 'Febraury', '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 ></div>

  • Related