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 –
- Laravel 9 to_route() and str() New Helper Function Tutorial
- How To Check if an Object is Empty in JavaScript Tutorial
- Autocomplete Places Search Box using Google Maps JavaScript API
- Remove All Whitespace From a String in JavaScript Tutorial
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.