Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Standards and Best Practices Databases and International Concerns Working with DateTime

Adrian Gordon
Adrian Gordon
9,008 Points

Did anyone else get a "Fatal error"?

Exact error: 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 the timezone 'UTC' for now, but please set date.timezone to select your timezone.' in /home/treehouse/workspace/index.php:3 Stack trace: #0 /home/treehouse/workspace/index.php(3): DateTime->construct('2014, August 23') #1 {main} thrown in /home/treehouse/workspace/index.php on line 3

1 Answer

Jose Soto
Jose Soto
23,407 Points

What was the code you were trying? Was it in a workspace? That information might help.

My guess is that when creating a new DateTime object, you need to specify the timezone. On a managed system, you could modify the php.ini date.timezone setting to your local timezone. However, we don't have that option with workspaces.

You can specify timezones when instantiating the DateTime object like this:

$date = new DateTime('now', new DateTimeZone('America/Los_Angeles'));

Hope that helps.

Daisho Komiyama
Daisho Komiyama
15,906 Points

It works for me. Thank you Jose!