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.
To work with the timezone offset, the PHP DateTimeZone object should be used with wp_date()
.
// 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, and several dependent features will break if the timezone is adjusted.
current_time( 'timestamp' )
is no longer recommended since WordPress 5.3.
For more in-depth guidance on working with timezones, refer to the well-written Make WordPress Core post: “Date/Time improvements.”
Last updated: March 07, 2024