0

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?

Baumr
  • 6,124
  • 14
  • 37
  • 63

5 Answers5

6

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.

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
  • Thank you, but no need to remind me to read the error message — this is an international website so it's probably best to not set a time zone? (In this case it's just a year, but in other cases it might be another date.) – Baumr Dec 20 '12 at 13:52
  • @Baumr No, you still need to set the timezone, you could probably use `UTC`. You could suppress error messages, but that's baddddd :) – Rudi Visser Dec 20 '12 at 13:53
  • I agree about suppressing error messages. The error says it's not safe, but how? – Baumr Dec 20 '12 at 13:58
  • 1
    @Baumr Because servers can generally be located anywhere in the world whilst serving the same backing codebase. If you were not to align your code across all of your "mirror" servers, you would be using different timezones depending on where they are. `UTC` is a good equaliser. – Rudi Visser Dec 20 '12 at 13:59
1
  1. Read and understand how the errors work in php: http://php.net/manual/en/function.error-reporting.php . Make yourself a method of errors that doesn't include showing it onyour page, but send it to file.

  2. fix the problem by doing what the warning says: see http://php.net/manual/en/function.date-default-timezone-set.php

  3. if you want to be crude, put an @ in front of the call that produces the warning. It surpresses errors/warnings.

Nanne
  • 64,065
  • 16
  • 119
  • 163
  • Thank you, but won't setting a time-zone reduce the "internationalness" of the time shown on the site? – Baumr Dec 20 '12 at 13:54
1

I would turn your html "inside-out" to solve your problem this way:

<?php
   date_default_timezone_set('America/Chicago');
   $displayDate = date("Y");
   print <<<HERE
     <div class="footer">
        <p id="left">Company Address</p>
        <p id="right">Copyright $displayDate Company Incorporated</p>
     </div>
HERE;
?>

You can do more complex things to setup your date text and get it "just right".

Alan Penny
  • 182
  • 1
  • 5
  • This still does not solve the problem if you were to omit `date_default_timezone_set`. – Rudi Visser Dec 20 '12 at 13:58
  • @RudiVisser, of course not, but it is a cleaner implementation than throwing in PHP inside HTML for this case. – Baumr Dec 20 '12 at 14:01
  • By the way, Alan, what are the two "HERE" labels there for? – Baumr Dec 20 '12 at 14:04
  • @Baumr I'd disagree, using a heredoc combined with `print` is not the best way to get around this. Having a `=date('Y')?>` is perfectly acceptable in this case if short tags are enabled (or running PHP 5.4+). Of course, using a full template would be even more preferred :) – Rudi Visser Dec 20 '12 at 14:05
  • So this is better? `` — works on localhost, but not on web server. – Baumr Dec 20 '12 at 14:09
  • @Baumr That's not particularly better, refer to my answer where it says to place it at the beginning of your script and/or a global include. What do you mean it doesn't work on the server? – Rudi Visser Dec 20 '12 at 14:13
  • @RudiVisser, but that is the beginning of the script, no? Also, with the above code, the web-server now literally just ignores anything that comes after that function. If I place it at the start of the whole page, then it doesn't render the whole page. (Yahoo Small Business Hosting don't even support `php.ini` or `.htaccess`, so this doesn't surprise me.) – Baumr Dec 20 '12 at 14:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21465/discussion-between-rudi-visser-and-baumr) – Rudi Visser Dec 20 '12 at 14:19
  • The "<<<" delineates a "here document" (see: http://en.wikipedia.org/wiki/Here_document ). The above code is not actually a function, and the server would not ignore anything after it. The code between the "" gets interpreted and run by the PHP engine and is performing essentially the same thing as the "", but allows for a bit more finessing of the formatting. – Alan Penny Dec 21 '12 at 14:09
0

Set date_default_timezone_set

http://php.net/manual/en/function.date-default-timezone-set.php

Niclas Larsson
  • 1,317
  • 8
  • 13
0

Please set default timezone in php.ini date.timezone

eg: date.timezone =  = America/Los_Angeles 
Vinoth Babu
  • 6,724
  • 10
  • 36
  • 55
  • Thanks, but the web host is terrible, and doesn't allow access to `php.ini` – Baumr Dec 20 '12 at 13:53
  • you can try using ini_set('date.timezone', 'America/Los_Angeles'); in your php code... place this in first line of your file – Vinoth Babu Dec 20 '12 at 13:54