PHP 5.1+ Date vs Strftime Timezone Annoyance

Posted by Chief on Feb 21, 2010 in Gotchas | Subscribe

In the old PHP4 days, you could change the default timezone by: putenv("TZ=America/Los_Angeles");. In php5, you can still do this and it’ll work for strtotime, strftime, gmstftime (maybe more). It will not work for methods date and gmdate. For those, starting in version 5.1, you’ll need to call date_default_timezone_set("America/Los_Angeles");. This is just another one of those annoying things that make PHP a dying language.

Together, to be cross-version compliant:

putenv("TZ=America/Los_Angeles");
if( function_exists("date_default_timezone_set") ) { 
 date_default_timezone_set("America/Los_Angeles");
}

We need both to be reverse-compatible with PHP4.

Tags: , , , ,

Copyright © 2010 cat brain.log | less All rights reserved.
Shades v1.2 theme from BuyNowShop.com.