388

I got this error when I requested to update the PHP version from 5.2.17 to PHP 5.3.21 on the server.

<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  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/New_York' for 'EDT/-4.0/DST' instead</p>
<p>Filename: libraries/Log.php</p>
<p>Line Number: 86</p>

</div>
Warning: 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/New_York' for 'EDT/-4.0/DST' instead in /filelocation right here/system/libraries/Log.php on line 86

Warning: 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/New_York' for 'EDT/-4.0/DST' instead in /filelocation right here/system/libraries/Log.php on line 99
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  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/New_York' for 'EDT/-4.0/DST' instead</p>
<p>Filename: libraries/Log.php</p>
<p>Line Number: 99</p>

</div>
Vinícius Fagundes
  • 1,983
  • 14
  • 24
user1852837
  • 4,441
  • 3
  • 16
  • 16

25 Answers25

588

You probably need to put the timezone in a configuration line in your php.ini file. You should have a block like this in your php.ini file:

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = America/New_York

If not, add it (replacing the timezone by yours). After configuring, make sure to restart httpd (service httpd restart).

Here is the list of supported timezones.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CtrlX
  • 6,946
  • 1
  • 17
  • 21
  • 13
    to be clear, after making this change, be sure to sudo service apache2 restart – Ryan Sep 19 '13 at 20:45
  • 2
    This solved it for me when other solutions did not. I only received this error when I used date functions on the same page as integration with Amazon SES. Maybe that will help someone else. – master_gracey Oct 07 '13 at 13:13
  • @DOOManiac +1 same here – willdanceforfun Jan 30 '14 at 18:44
  • @CtrlX: I retract my comment; it was user error on my part (I was editing the wrong copy of php.ini) – DOOManiac Feb 03 '14 at 16:52
  • 10
    If you are using PHP CLI (php -r or php -a) within your client terminal, you need to edit the CLI php.ini file, not apache2. YourC CLI php.ini file can usually be found at /etc/php5/cli/php.ini – justinpage Apr 02 '14 at 00:23
  • 1
    If you are using php-fpm - it's also need to be restarted: sudo service php-fpm restart – Yuri Gor Jun 04 '14 at 17:43
  • 2
    Like the first comment about Apache you must also restart IIS for this to take effect on Windows/IIS. – jross Aug 25 '14 at 00:03
  • 2
    This solve my problem! Just an info for the non-experts like me, in order to find the `php.ini` used by apache, just execute the following command `php -i | grep php.ini`. – Joël Salamin Sep 24 '14 at 20:15
  • 1
    @JoëlSalamin, You could have just done `php --ini`. It will also give you additional information regarding potential errors. – Pacerier Mar 18 '15 at 04:21
  • If I use this solution. The result is 500 internal server errror. – Hafiz Shehbaz Ali Dec 15 '15 at 07:02
  • @Shehbaz Ali : Did you wrote correctly the timezone name. If not, apache won't be able to start – CtrlX Dec 19 '15 at 20:24
  • I wrote it and restart the server from the service componenet in window 10. but, then it was giving 500 server error. – Hafiz Shehbaz Ali Dec 20 '15 at 20:21
  • the same thing is working in my LAMP. On the window, I installed apache, PHP, MySQL from .msi file and configure them manually. the same code is not working. – Hafiz Shehbaz Ali Dec 20 '15 at 20:22
  • @ShehbazAli : You should look into your apache error log to find out why you have that HTTP 500 error – CtrlX Dec 23 '15 at 04:48
  • This answer works if your server and account is setup to let you use a php.ini file. However, it did not work for me at first because the php.ini file was not recursive (did not apply to subfolders). So I also had to edit my .htaccess file and add this line: suphp_configpath /home/your_name/public_html - where your_name is your account name – Jeff Baker Aug 03 '17 at 21:48
213

If you cannot modify your php.ini configuration, you could as well use the following snippet at the beginning of your code:

date_default_timezone_set('Africa/Lagos');//or change to whatever timezone you want

The list of timezones can be found at http://www.php.net/manual/en/timezones.php.

Babatunde Adeyemi
  • 14,360
  • 4
  • 34
  • 26
  • 8
    For some reason setting date.timezone = America/New_York in my php.ini and restart apache did not stop this error message when running a script using swiftmailer. I ended up also having to add date_default_timezone_set('America/New_York'); at top of script as suggested. Thanks! – kyle Dec 15 '13 at 20:59
  • I tried your solution. Now, error is 500 internal server error. I am using apache 2.x and codeigniter . – Hafiz Shehbaz Ali Dec 15 '15 at 07:03
  • @ShehbazAli You must have done something wrong. In which of your scripts did your add the snippet? – Babatunde Adeyemi Dec 28 '15 at 19:50
  • the same code is working on the production. There is no error. I have installed the apache, php and MySQL manually on window 10. PHP, MySQL and Apache work. But, this project is giving error. – Hafiz Shehbaz Ali Dec 29 '15 at 17:18
  • Wow. This worked like magic. Thanks my brother. – Estherons Samuel Mar 22 '21 at 19:52
77

Add the following in your index.php file. I first came across this when I moved my application from my XAMPP server to Apache 2.2 and PHP 5.4...

I would advise you do it in your index.php file instead of the php.ini file.

if( ! ini_get('date.timezone') )
{
    date_default_timezone_set('GMT');
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Justis Matotoka
  • 809
  • 6
  • 3
  • 3
    excellent solution for when you don't necessarily have full system access, guess this one is for people with limited access or need a more robust solution, and combining this with editing the php.ini if you can – aqm Mar 25 '14 at 08:37
18
<? print(gmdate("Y")); ?> 

instead of

<? print(date("Y")); ?>

worked for me (shows current year and no more shows the error message). (Thanks to Chris above)

Susan Taunton
  • 181
  • 1
  • 2
16

If these are not your options

  • Modifying php.ini.
  • Adding date_default_timezone call.

Instead of date you could use gmdate.

I have used gmdate( "Y" ) when I needed a year for a copyright snipplet.

Uours
  • 2,517
  • 1
  • 16
  • 21
Chris
  • 5,664
  • 6
  • 44
  • 55
15

If you are using CodeIgniter and can't change php.ini, I added the following to the beginning of index.php:

date_default_timezone_set('GMT');
Pang
  • 9,564
  • 146
  • 81
  • 122
Jose
  • 266
  • 3
  • 4
  • It works for me, thanks Jose for your help. Best Regards, Sarim Ghani http://www.i-splendid.com – Sarim Shekhani Nov 12 '16 at 23:27
  • This works on native php http://php.net/manual/en/function.date-default-timezone-set.php. TY, you put me on the right track. In relation see timezone list for the function here http://php.net/manual/en/timezones.php – Louis Loudog Trottier Feb 28 '17 at 04:17
14

I always keep this line inside codeigniter's root index.php.So that my code works at any server

date_default_timezone_set('Asia/Dhaka');

List of Supported Timezones here

Shaiful Islam
  • 7,034
  • 12
  • 38
  • 58
11

@Justis pointed me to the right direction, but his code did not work for me. This did:

// set the default timezone if not set at php.ini
if (!date_default_timezone_get('date.timezone')) {
    // insert here the default timezone
    date_default_timezone_set('America/New_York');
}

Documentation: http://www.php.net/manual/en/function.date-default-timezone-get.php

This solution is not only for those who does not have full system access. It is necessary for any script when you provide it to anyone else but you. You never know on what server the script will run when you distribute it to someone else.

John Linhart
  • 1,746
  • 19
  • 23
5

I had to put it in double quotes.

date_default_timezone_set("America/Los_Angeles"); // default time zone
Pang
  • 9,564
  • 146
  • 81
  • 122
nocturnalact
  • 71
  • 1
  • 2
  • Still no workie (in Mac OSX), so I eventually broke down and added `/etc/php.ini: date.timezone = America/Los_Angeles` – MarkHu Apr 28 '16 at 18:52
5

This issue has been bugging me for SOME time as im trying to inject a "createbucket.php" script into composer and i keep being told my time-zone is incorrect.

In the end the only thing that fixed the issue was to: $ sudo nano /etc/php.ini

Search for timezone

[Date]
; Defines the default timezone used by the date functions
; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
date.timezone = UTC

Ensure you remove the ;

Then finally

$ sudo service httpd restart

And you'll be good to go :)

Rohan Khude
  • 4,455
  • 5
  • 49
  • 47
T0mmyKn1fe
  • 61
  • 1
  • 3
4

You can set the timezone in your .htaccess file

php_value date.timezone UTC
rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
2

In addition to setting the date.timezone= as mentioned in several answers, I found an error in the php.ini file that was keeping it from getting to the date.timezone. The way I found it was by running php from the command line in a terminal. this caused an error to be reported at line 114. In my case I had uncommented a setting for displaying errors that had '|' between 2 values. It did not like it. I removed one of the values and the | and everything was good after that

M Graham
  • 21
  • 1
2
<? date_default_timezone_set('Europe/Istanbul'); ?>

For php (or your location).

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
1

A simple method for two time zone.

<?php 
$date = new DateTime("2012-07-05 16:43:21", new DateTimeZone('Europe/Paris')); 

date_default_timezone_set('America/New_York'); 

echo date("Y-m-d h:iA", $date->format('U')); 

// 2012-07-05 10:43AM 
?>
Saurabh Chandra Patel
  • 12,712
  • 6
  • 88
  • 78
1

For docker user: Create a local timezone.ini file in your project, set up the config in docker-compose.yml,

    volumes:
        - "./docker/config/timezone.ini:/usr/local/etc/php/conf.d/timezone.ini"

timezone.ini

    date.timezone=Australia/Sydney
Jin
  • 665
  • 1
  • 7
  • 15
0

A quick solution whilst your rectify the incompatibilities, is to disable error reporting in your index.php file:

Insert the line below into your index.php below define( ‘_JEXEC’, 1 );

error_reporting( E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR |
E_COMPILE_WARNING );
0

I am hosting my EC2 and S3 bucket in us-west-2 (Oregon) region. When I was calling $s3client->listBuckets() to list existing buckets in my php, I was getting exception - "Uncaught exception 'Exception' with message 'DateTime::__construct(): It is not safe to rely on the system's timezone settings...". I made below changes to make it work. Sharing these details in case someone is facing similar issue and any of above answers have not helped.

  1. Based on documentation @ AWS Configuring Network Time Protocol (NTP), I confirmed ntpd service status is fine by running ntpstat command. If you get error, this link helps you to know what is going wrong.
  2. Opened /etc/php.ini file (some may have it in different path based on version of php installed) and found that date.timezone was not set to any value and also it was commented by default. I un-commented by removing ';' before this line and set its value to "UTC" and saved the file.
  3. Restarted http and ntp daemons using sudo service httpd restart and sudo service ntpd restart commands.

After this I am able to list buckets successfully without any exception. Hope this helps.

Maruti C
  • 11
  • 2
0

If you using Plesk, try it, First, Open PHP Settings, at the bottom of page, change date.timezone from DEFAULT to UTC.

enter image description here

enter image description here

Latief Anwar
  • 1,833
  • 17
  • 27
0

I had this error running php-fpm in a chroot jail. I tried creating etc/php.ini and /usr/share/zoneinfo in the chroot directory, but it just didn't work. I even tried strace-ing the php-fpm daemons to see what file they were missing - nothing jumped out.

So in case Google brings you here because you get this error when using php-fpm configured for chroot, you can probably fix it by adding this line to /etc/php-fpm.d/www.conf in the ENV section:

env[TZ] = America/New_York

A php-fpm restart is normally required for it to take effect. Hope this helps somebody out there.

Don Law
  • 1,329
  • 13
  • 7
0

In my particular case, I have PHP configured to use PHP-FPM (FastCGI Process Manager). When executing phpinfo() from CLI I saw correct timezone that I had set in the php.ini, however it was still incorrect in the browser and causing my code to fail. I simply needed to restart the php-fpm service on the server.

service rh-php56-php-fpm restart

You may also need to restart the httpd service if you edited the php.ini

service httpd restart
EternalHour
  • 8,308
  • 6
  • 38
  • 57
0

This answer above from CtrlX is the correct answer, but it may not work completely. I added this line to my php.ini file:

date.timezone = "America/Los_Angeles"

but it did not remove the PHP error for all my files because some of my PHP scripts are in subfolders. So I had to edit .htaccess file to setup php.ini to be used recursively (in subfolders):

suphp_configpath /home/account_name/public_html

where account_name is your cpanel account name and public_html is the folder your php.ini file is in.

Jeff Baker
  • 1,492
  • 1
  • 12
  • 15
0

If you don't have access to the file php.ini, create or edit a .htaccess file in the root of your domain or sub and add this (generated by cpanel):

<IfModule mime_module>
AddType application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>

<IfModule php5_module>
php_value date.timezone "America/New_York"
</IfModule>

<IfModule lsapi_module>
php_value date.timezone "America/New_York"
</IfModule>
Ignatius
  • 2,745
  • 2
  • 20
  • 32
  • Please try not to use capital words in case that normal casing convention can express what you want to say. – shizhen Jul 25 '19 at 02:01
0

There are two options for the resolve this

First, change into the php.ini file and put default timezone

date.timezone = "America/New_York"

Once, you have set the timezone in php.ini restart the server

Secondly, Change the run time assign time zone based on your need

date_default_timezone_set('America/New_York');
Nikunj K.
  • 8,779
  • 4
  • 43
  • 53
0

I've done or created this in the .htaccess file:

php_value date.timezone Asia/Jakarta

But it doesn't work. Then I tried to upgrade the php version from 5 to 7. And it solved the problem.

Hana Hasanah
  • 145
  • 1
  • 6
-1

In your connection page put a code like this date_default_timezone_set("Africa/Johannesburg"); based on your current location.

h3t1
  • 1,126
  • 2
  • 18
  • 29
CuteDevIT
  • 1
  • 1