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

Robert Walker
Robert Walker
17,146 Points

php timezone and server timezone issue

My server is using an American timezone and sadly can not be changed, the problem is that I have a account created date on my website and being that my timezone is London the account created time and date is always wrong.

Is there a way to fix this, ive seen a few articles say to change the server timezone but I sadly don't have access to that with my current host.

3 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Robert,

I recommend you have a look into Standards and Best Practices, specifically Databases and International Concerns which goes over managing dates using the server time but outputting them in correct timezones.

Hope that helps.

Robert Walker
Robert Walker
17,146 Points

Not really understanding how that's going to help me here as im trying to input a SQL query using NOW().

Also from the database and international concerns video you would have to cater to every single timezone that people would come from, that seems crazy to me, so you would need to build a whole database section for users to store their own timezone to then convert every date and time on your site to each timezone every time someone loads a page.

Every forum post, comment and login times would need to be converted every time someone loaded the page?

Am I missing something here?

kevin jordan
kevin jordan
11,353 Points

Hey Robert -

Just a thought - couldn't you add/subtract a certain number of hours from your Date calls ?

example code taken from : http://php.net/manual/en/datetime.add.php

<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?>

You would have to work with the format a bit, but it might be a start !

Best, kj

We can use ini_set ('date_default_timezone_set ') for changing the timezone in the server for your applicaton.

<?php
 ini_set ('date_default_timezone_set ' , 'UTC') ;
?>