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

Bill Dowd
Bill Dowd
7,681 Points

How can the timezone be set for my Web application in PHP?

How can the time zone be set for my Web application in PHP? Can I set it in one place and have it maintained throughout the entire application, even when it's accessed in different time zones or must I set it on each new page load?

Bill Dowd
Bill Dowd
7,681 Points

Thank you Hannah. So if I use the line: date_default_timezone_set('Europe/London'); in a foundational class file, will that work for throughout the program? In other words, does that line of code need to run on every page load? Also, will this alter all my date() functions throughout the application to read the Unix time from that time zone?

2 Answers

<?php
date_default_timezone_set('Europe/London');

if (date_default_timezone_get()) {
    echo 'date_default_timezone_set: ' . date_default_timezone_get() . '<br />';
}

if (ini_get('date.timezone')) {
    echo 'date.timezone: ' . ini_get('date.timezone');
}

?>
Bill Dowd
Bill Dowd
7,681 Points

Thank you Hannah. I setup a DB table with the time zones from around the world. It was easy to find an already prepared sql query for that. I setup a function and form to select the zone from a drop-down menu, stored the value in a configuration table, created a function to call that field from the table, and returned the value to your first line: date_default_timezone_set($config_field);. Then I placed that line in my session management file. It seems to have taken care of all the time problems except the shared host server time. Can't do anything about that until I move to VPS or dedicated hosting.