skip to Main Content

For each page I am coding I have a foot that I use php include "footer.php" in all of them. Below is the foot.php code. I need my code to be able to give the correct time stamp for each file not one speific file. So in the end each page should have a different time stamp because I am workign on each file at different times becasue the website I am making is for a class assignments. And of course each assignment is being coded at on different days. If this doesn’t make since let me know.

<footer align="center">
    <hr width="900px" size="2" noshade="noshade" color="black" align="center">
    <div style="padding:0 30px">
        <p>Validated by:</p>
        <a href="https://validator.w3.org/check?uri=referer"><img src="https://www.w3.org/Icons/valid-xhtml11" alt="Valid XHTML 1.1" height="31" width="88"></a>
        <a href="https://jigsaw.w3.org/css-validator/check/referer"><img src="https://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>

        <p>Last modified:
            <?php
            $timestamp = filemtime(__FILE__);
            $date_time = new DateTime();
            $date_time->setTimezone(new DateTimeZone("America/New_York"));
            $date_time->setTimestamp($timestamp);

            echo $date_time->format("F j Y g:i a.");
            ?>
        </p>
    </div>
</footer>

I asked for help on this before but not working out.

2

Answers


  1. First we need a function that reads all files of a folder in an association
    reads out the timestamp values

    function getFileModificationTimes(string $directory): array {
        $files = scandir($directory);
        $modificationTimes = [];
        foreach ($files as $file) {
            if ($file === '.' || $file === '..') {
                continue;
            }
            $path = $directory . '/' . $file;
            if (!is_file($path)) {
                continue;
            }
            $modificationTimes[$file] = filemtime($path);
        }
        return $modificationTimes;
    }
    

    and a second function that determines the largest/most recent value

    function getMaxModificationTime(array $modificationTimes) {
        $maxModificationTime = 0;
        $maxFile = '';
        foreach ($modificationTimes as $file => $modificationTime) {
            if ($modificationTime > $maxModificationTime) {
                $maxModificationTime = $modificationTime;
                $maxFile = $file;
            }
        }
        return [$maxFile, $maxModificationTime];
    }
    

    the footer now shows the date of the newest file from the folder

    <?php
         $modificationTimes = getFileModificationTimes('/path/to/directory');
         list($maxFile, $maxModificationTime) = getMaxModificationTime($modificationTimes);
    ?>
    
    <footer align="center">
        <hr width="900px" size="2" noshade="noshade" color="black" align="center">
        <div style="padding:0 30px">
            <p>Validated by:</p>
            <a href="https://validator.w3.org/check?uri=referer"><img src="https://www.w3.org/Icons/valid-xhtml11" alt="Valid XHTML 1.1" height="31" width="88"></a>
            <a href="https://jigsaw.w3.org/css-validator/check/referer"><img src="https://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
    
            <p>The file with the most recent modification time is <?=$maxFile?>, which was modified at <?=date('Y-m-d H:i:s', $maxModificationTime)?>.
            </p>
        </div>
    </footer>
    
    
    Login or Signup to reply.
  2. If you want to show the modification time of the file including the footer, then remove $timestamp = filemtime(__FILE__); from the footer file and put it before the include().

    Eg:

    $timestamp = filemtime(__FILE__);
    include('footer.php');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search