Jump to content
Larry Ullman's Book Forums

Script 16.5 Exception With Datetime::___ Construct()


Recommended Posts

Datetime script returns:

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): 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/Anguilla' for 'AST/-4.0/no DST' instead' in /var/www/html/testing/datetime.php:16 Stack trace: #0 /var/www/html/testing/datetime.php(16): DateTime->__construct() #1 {main} thrown in /var/www/html/testing/datetime.php on line 16

timezone is set to "Canada/Atlantic" in php.ini

Link to comment
Share on other sites

Edit: Looks like I posted too soon.

 

Seems like "Canada/Atlantic" is depricated [link]. The timezone might not be available. I'm pretty sure you should be able to find another one that fits you in the America section of the timezones: http://no2.php.net/manual/en/timezones.america.php

 

As a general suggestion, you should also look out for exception:

try {
    $dateTime = new DateTime("INVALID");
    echo $dateTime;
}
catch ( Exception $e ) // Error handling
{
    echo "Invalid time";
}

You can solve this a couple of ways.

 

1. Edit your php.ini file: (location depends on operating system, so search the web for common locations for you)

- Find "date.timezone", (around line 1045 for me, but search for it) make sure it's set to "America/Anguilla" (or what your default may be) and make sure the line is not commented out. (";" - semi-colons creates a comment)

 

2. Use date_default_timezone_set()

- Has be called before you create a datetime object

date_default_timezone_set("America/Anguilla")
Edited by Antonio Conte
Link to comment
Share on other sites

 Share

×
×
  • Create New...