Time zones and PHP question ...
July 27, 2009 7:05 AM   RSS feed for this thread Subscribe

PHP gurus: please help me do something specific with the date function.

So my website uses some logic to determine what time of day/day of week it is, and display content accordingly. It all hinges on this variable:

$now = date('l H:i');

Over the weekend, we moved web hosts - to a company in California, and now the dynamic content is always three hours off. I've looked up the documentation on the function, and it makes my head hurt. What do I do to that variable assignment to add three hours to the result it returns?
posted by jbickers to computers & internet (5 comments total)
The hackish way is to just add 3 hours to the result of time() e.g., $now = date('l H:i', time()+(3600*3));

This is not the most correct way to do it, and if you switch timezones again, you'll have to change the code again. What you should really do is use this function to change your timezone to "America/New_York".
posted by mkb at 7:19 AM on July 27


Awesome ... thanks! That saves the day.
posted by jbickers at 7:36 AM on July 27


You can either change the defeault timezone:
date_default_timezone_set('UTC');
or specify the timezone when you call date, something like:
$now = date('l H:i UTC');
posted by devnull at 7:39 AM on July 27


You can also use a datetime object

$EastCoast = new DateTimeZone("America/New_York");
$now = new DateTime("now",$EastCoast);
$now->format("l H:i");
posted by Nothing at 8:41 AM on July 27


It would be much simpler -- and you wouldn't have to modify any of your code -- if you could just request your web host to change the server time to Eastern instead of Pacific.

Several of my largest clients are in New York, but most of my servers are in Portland. The servers are just set to Eastern time.
posted by GatorDavid at 10:05 AM on July 28


« Older How can I host a member direct...   |   Can anyone tell me the brand a... Newer »

You are not logged in, either login or create an account to post comments