Ashburn, June 27, 2017

How To Synchronize Timezones in PHP and MySQL in WordPress

How To Synchronize Your Timezones in PHP and MySQL in Wordpress
How To Synchronize Your Timezones in PHP and MySQL in Wordpress

In this tutorial I will show you how to synchronize timezones in PHP and MySQL in wordpress.

Luckily, you can do it by adding a few lines of codes to your PHP MySQL application.

Open your functions.php file or plugin, and copy and paste the following snippet:

add_action( 'init', 'kmb_sync_php_and_mysql_timezones' );
function kmb_sync_php_and_mysql_timezones(){
	global $wpdb;

	$now = new DateTime();
	$mins = $now->getOffset() / 60;
	$sgn = ($mins < 0 ? -1 : 1);
	$mins = abs($mins);
	$hrs = floor($mins / 60);
	$mins -= $hrs * 60;
	$offset = sprintf('%+d:%02d', $hrs*$sgn, $mins);
	$query = "SET time_zone='".$offset."'";
	$wpdb->get_results( $query );	


You will need to replace ‘America/New_York‘ by your desired timezone.

Thus, you might also find helpful to check the whole PHP list of supported timezones.

The PHP and MySQL timezones are now synchronized within your WordPress application. No need to go for php.ini or MySQL console!

I hope it helps you!


About Karlos Maldonado 36 Articles
Computer Sciences Engineer graduated from Florida Atlantic University (FAU) in Boca Raton, Florida. Has gotten great experience in the IT and Web Development industry working for companies such as SIRIUS XM RADIO, 4MEDIA as well as collaborating with local companies.

Be the first to comment

Leave a Reply

Your email address will not be published.