How to Convert Local Time to UTC in PHP
In this tutorial, We have shared with you how to convert the date and time value from one timezone to another timezone using PHP DateTime and DateTimeZone classes. You can convert any timezone to UTC DateTime using PHP.
We will show you how to convert local date&time to UTC DateTime (YYYY-MM-DD HH:MM:SS AM/PM format) in PHP.
Convert Current Date&Time to UTC Date&Time
1 2 3 4 5 6 7 |
<?php $currentDateTime = date("Y-m-d H:i:s A"); $newDateTime = new DateTime($currentDateTime); $newDateTime->setTimezone(new DateTimeZone("UTC")); $dateTimeUTC = $newDateTime->format("Y-m-d h:i A"); echo $dateTimeUTC; ?> |
Convert Local Date&Time to UTC Date&Time
1 2 3 4 5 6 7 8 |
<?php $dateTime = '2021-11-19 14:58:55'; $timezone_from = 'America/New_York'; $newDateTime = new DateTime($dateTime, new DateTimeZone($timezone_from)); $newDateTime->setTimezone(new DateTimeZone("UTC")); $dateTimeUTC = $newDateTime->format("Y-m-d h:i A"); echo $dateTimeUTC; ?> |