PressTigers

How to modify Time Zone Settings in WordPress

In WordPress, time zone settings are being used for the date and time of published posts and pages. Basically, WordPress uses the standard UTC time difference to calculate the time zone and show the time and date of the post. By default, there is no option to get the time zone through which you can show the general time on a website. If you will use the PHP date function for this purpose, it will only show the server date and time.

You can set the time zone using PHP ini settings, but that will change the entire server time and WordPress time zone settings will no longer be effective.

Solution:

There is a simple hack which you can use to show the time on your website according to the preset time zone in WordPress settings. This can be achieved using the PHP DateTime and DateTimeZone object.

You need to initialize a new DateTime object and set the time zone string within the parameters of DateTimeZone object, passed as a parameter to the DateTime object:

To get the match of time zone string with the option set in WordPress settings, you need to use the get_option() WordPress function. The complete time zone function will become like this:

By using the $timezone object, you can show the date and time according to the chosen time zone in WordPress settings. You can access any date format same like WordPress datetime or PHP date format as shown below:

Here $timezone->format( ‘Y-m-d H:i:s’ ) will do the trick and show the date and time according to the WordPress preset timezone.

User Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Get in Touch