Skip to content

Code quality and best practices

Obtaining a local time

To retrieve the current time as an object with the timezone from the WordPress Admin settings, use the current_datetime() function (WordPress.org code reference).

To work with the timezone offset, the PHP DateTimeZone object should be used with wp_date() (WordPress.org code reference).

// usage example of DateTimeZone() and wp_date()
$timezone = new DateTimeZone( 'Asia/Singapore' );
wp_date("d-m-Y H:i:s", null, $timezone );

Do not use date_default_timezone_set(). WordPress core requires the timezone in PHP to be GMT+0. Several features are dependent on this, and will break if the timezone is adjusted.

current_time( 'timestamp' ) is no longer recommended since WordPress 5.3.

A well written Date/Time improvements post is available for more in-depth explanations for working with timezones.

Last updated: May 06, 2021