PHP How To Convert Date Time From One Timezone To Another

Share this Article
Reading Time: 3 minutes
423 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.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5