Search code examples
phpdatelocalhostechocoda

PHP include combined with 'echo date' doesn't work in localhost?


I am using the following on all my index.php files to include a global footer:

<?php include($_SERVER['DOCUMENT_ROOT'].'/_footer.php'); ?>

The _footer.php file itself doesn't have much content apart from the PHP to return the current year:

    <div class="footer">
        <p id="left">Company Address</p>
        <p id="right">Copyright <?php echo date("Y"); ?> Company Incorporated</p>
    </div>

</div> <!-- end .wrapper -->

</body>
</html>

While the above code works when uploaded to the web server, it does not work in my localhost (Mac OS X with Coda 2) and I get the error message:

WARNING: DATE() [FUNCTION.DATE]: IT IS NOT SAFE TO RELY ON THE SYSTEM'S TIMEZONE SETTINGS. YOU ARE REQUIRED TO USE THE DATE.TIMEZONE SETTING OR THE DATE_DEFAULT_TIMEZONE_SET() FUNCTION. IN CASE YOU USED ANY OF THOSE METHODS AND YOU ARE STILL GETTING THIS WARNING, YOU MOST LIKELY MISSPELLED THE TIMEZONE IDENTIFIER. WE SELECTED 'UTC' FOR 'GMT/0.0/NO DST' INSTEAD IN /USERS/USER/SITES/COMPANY/EXAMPLE.COM/_FOOTER.PHP ON LINE 3

I would posit that it wouldn't be such a problem to set the timezone or not (since it's a simple function designed to show the year only), but it breaks the design of the footer when local testing:

enter image description here

All the text (e.g. "COPYRIGHT") is meant to be inside that yellow box.

What should I do in this case?


Solution

  • Have you read the error message? It quite clearly states that it is not safe to rely on the system's timezone settings, and that you are required to use the Date.Timezone setting or the date_default_timezone_set() function.

    So, do one or the other.

    Either a) Modify your php.ini to have this:

     date.timezone=Europe/London
    

    or b) Call this above your call to date() (or in a globally included script):

    date_default_timezone_set('Europe/London');
    

    Obviously, Europe/London should be replaced with your actual timezone identifier of which a list is available here: http://php.net/manual/en/timezones.php

    Just an additional note, if it's not showing on your server it's because either display_errors is disabled, or your error reporting level is lower than it is in your development machine, which it certainly should be.