PHP How To Convert Date Time From One Timezone To Another

Reading Time: 3 minutes
1,116 Views

Inside this article we will see the concept i.e PHP How To Convert Date Time From One Timezone To Another Tutorial. Article contains the classified information about conversion of date time from one timezone to other.

If you are looking for a solution i.e How to Convert Date and Time from one timezone to another in php then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Learn More –

Let’s get started.

How To Get & Set Timezone

To set timezone in PHP, you need to use a PHP Datetime function i.e

date_default_timezone_set("TIMEZONE");

The date_default_timezone_set() function sets the default timezone used by all date/time functions in the script.

Example

<?php

date_default_timezone_set("Asia/Bangkok");

echo date_default_timezone_get();

?>

DateTime Conversion From a Timezone to Other

This is a very basic concept of PHP to convert a datetime from one timezone to other.

In PHP, we have a class provided i.e DateTime. It provides several method by which we can work with timezones in a very easier way.

Let’s see a PHP program,

<?php

$date = new DateTime('2022-08-12 12:45:00');

$date->setTimezone(new DateTimeZone('America/Chicago'));
echo "<h3>Chicago Time: " . $date->format('Y-m-d H:i:s') . "</h3>";

$date->setTimezone(new DateTimeZone('America/New_York'));
echo "<h3>New_York Time: " . $date->format('Y-m-d H:i:s') . "</h3>";

$date->setTimezone(new DateTimeZone('Asia/Dubai'));
echo "<h3>Dubai Time: " . $date->format('Y-m-d H:i:s') . "</h3>";

Output

Available Timezones List

Here,

This is the list of available timezones.

Click here to go to the list of timezones.

We hope this article helped you to learn PHP How To Convert Date Time From One Timezone To Another Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more