skip to Main Content

PHP coding question

How do I add DateTimeZone (‘America/NewYork’) to the PHP code at the end? I need my code to use filemtime. I included this footer.html in my main files so that all my website pages have the same footer. I also need the footer to filemtime the current file and not just header.html. If you know how to fix that too, please help me out.

<footer>

<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 echo " ".date("F j Y g:i a.", filemtime("header.html")); ?>

</p>

</div>

</footer>

I tried rewritting the whole thing but nothing worked. As in nothing showing up at all or the time is 0:0.0

2

Answers


  1. What you want, if I understand you correctly is to output the timestamp of filemtime("header.html") in the timezone of America/NewYork.

    This is how you would do that:

    $timestamp = filemtime("header.html");
    
    $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.");
    

    Note that it is America/New_York and not America/NewYork.

    If you want to display the filemtime of the current file you can use __FILE__:

    $timestamp = filemtime(__FILE__);
    
    Login or Signup to reply.
  2. filemtime() produces a Unix time, which is not subject to time zones (it’s a fixed moment in time). All PHP functions that operate on Unix time and require a time zone (e.g. date()) read it from the global value. Such value can be set with the date.timezone directive or the date_default_timezone_set() function.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search