17

How to Convert Local DateTime to UTC in PHP

 3 years ago
source link: https://www.codexworld.com/how-to/convert-local-datetime-timezone-to-utc-in-php/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Home  »  How To Guides  »  PHP   »   How to Convert Local DateTime to UTC in PHP

How to Convert Local DateTime to UTC in PHP


Timezone conversion to a specific timezone helps to synchronize different DateTime. It is very useful to balance the server time and the user’s local time. PHP DateTime class provides an easy way to convert a date time stamp to UTC. You can convert any timezone to UTC DateTime using PHP.

In the example code snippet, we will show you how to convert local date&time to UTC DateTime (YYYY-MM-DD HH:MM:SS format) in PHP.

Convert Current DateTime to UTC DateTime using PHP:

$dateTime = date("Y-m-d H:i:s"); 
$newDateTime = new DateTime($dateTime);
$newDateTime->setTimezone(new DateTimeZone("UTC"));
$dateTimeUTC = $newDateTime->format("Y-m-d H:i:s");

Convert Local DateTime to UTC DateTime using PHP:

$dateTime = '2021-04-28 18:37:54'; 
$tz_from = 'America/New_York';
$newDateTime = new DateTime($dateTime, new DateTimeZone($tz_from));
$newDateTime->setTimezone(new DateTimeZone("UTC"));
$dateTimeUTC = $newDateTime->format("Y-m-d H:i:s");

Leave a reply

Cancel

Comment*

Your Name*

Your Email*

Your Website


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK