1
votes

Little problem with my php code, please see below

<?php
$thisyear = date('Y');
echo "<span>&copy; 2004-$thisyear All rights reserved. </span>";
?>

The error I am getting in the error_log is of the following nature

PHP 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 'America/Chicago' for 'CST/-6.0/no DST'

I have no idea how to specify either of this in my simple php script.

Any help is appreciated!

6
date_default_timezone_set() function needs to be set with the timezone that's all.Lenin

6 Answers

6
votes

I believe the warning is as explicit as it gets. Try and use this:

<?php
date_default_timezone_set('America/Los_Angeles');
$thisyear = date('Y');
echo "<span>&copy; 2004-$thisyear All rights reserved. </span>";
?>

You can see a list of all supported time zones following this link: http://php.net/manual/en/timezones.php

2
votes

The documentation is really quite clear.

You do

date_default_timezone_set('UTC');

(Or whatever timezone you want)

1
votes

You simply need to specify the timezone like so.

<?php
date_default_timezone_set('America/Chicago'); // Or any other timezone

$thisyear = date('Y');
echo "<span>&copy; 2004-$thisyear All rights reserved. </span>";
?>

For a complete list of timezones see - List PHP TimeZones

1
votes
    <?php
    // set the default timezone to use. Available since PHP 5.1
    date_default_timezone_set('UTC');
   echo date('y');
    ?>
0
votes
 date_default_timezone_set("Europe/Rome");
0
votes

If you are using PHP>=5.4.0 you have two options:

  1. Use the date_default_timezone_set() before calling the date function

    < ? php

    date_default_timezone_set("America/Chicago");

    $thisyear = date('Y');

    echo "© 2004-$thisyear All rights reserved. ";

    ?>

  2. Use the settings to specify a default timezone in your php.ini by adding the option date.timezone in your php.ini file and simply use the date function:

    [Date]

    ; Defines the default timezone used by the date functions

    date.timezone = America/Chicago

Option 2 is a default value and it will be used unless you explicitly call date_default_timezone_set() before calling date.

If your PHP version is <5.4.0 you can also use the environment variable TZ or the operating system. In the date_default_timezone_set() documentation you can see the precedence:

In order of preference, this function returns the default timezone by:

  • Reading the timezone set using the date_default_timezone_set() function (if any)

  • Prior to PHP 5.4.0 only: Reading the TZ environment variable (if non empty)

  • Reading the value of the date.timezone ini option (if set)

  • Prior to PHP 5.4.0 only: Querying the host operating system (if supported and allowed by the OS). This uses an algorithm that has to guess the timezone. This is by no means going to work correctly for every situation. A warning is shown when this stage is reached. Do not rely on it to be guessed correctly, and set date.timezone to the correct timezone instead.

Hope it helps