[wp-testers] Date() function

Otto otto at ottodestruct.com
Wed Apr 21 17:48:50 UTC 2010


On Mon, Apr 19, 2010 at 3:07 AM, Luke <notfornoone at gmail.com> wrote:
> I have found that if you are on PHP5 doing the following seems to make
> PHP's native date and time functions work as expected:
>
> 1. First set your timezone in Wordpress settings.
> 2. Then add something like the following:
>
> function my_init() {
>  if( get_option( 'timezone_string' ) != '' ) {
>    date_default_timezone_set( get_option( 'timezone_string' ) );
>  }
> }
> add_action( 'init', 'my_init');
>

Best not to do this, due to plugin and other things going wonky.

Basically, in order to maintain consistency across older PHP 4
installations, it was decided to have WP force the PHP environment to
be using UTC dates for everything. Then WordPress itself applies the
adjustments when needed.

Too many WP plugins (and core code) expect the date functions to
return UTC dates. If you change it, you may see odd behaviors and
date/time displays in plugins and other places.

A call to current_time( 'timestamp' ) will give you the current
timestamp adjusted by the necessary amount to get the correct time
display. So to get the correct date in WordPress using date(), you'd
do this:

date('Y-m-d', current_time( 'timestamp' ));

How it breaks down:
- PHP date and time functions should always give you the info in UTC.
- current_time( 'timestamp' ) will give you a UTC timestamp adjusted
by the amount necessary to show the local date.

-Otto


More information about the wp-testers mailing list