PHP - Date Time Conversion

version : PHP
Post Time : 28-06-2024

PHP - Date Time Conversion


Local To UTC conversion


                                      
                                        

<?php

/*--------Local To UTC-----*/
date_default_timezone_set('Asia/Kolkata');
$localDate = date("Y-m-d H:i:s");
$localDateTime = new DateTime($localDate, new DateTimeZone('Asia/Kolkata'));
$localDateTime->setTimezone(new DateTimeZone('UTC'));
$newUTCDate = $localDateTime->format('Y-m-d H:i:s');
echo $newUTCDate;


UTC To Local Conversion


                                      
                                        

<?php

/*---------UTC To Local Timezone--------*/
$TimeZone  = 'UTC';
$UTCDate = date("Y-m-d H:i:s");
$convertTimeZone = 'Asia/Kolkata';
$UTCDateTime = new DateTime($UTCDate, new DateTimeZone($TimeZone));
$UTCDateTime->setTimezone(new DateTimeZone($convertTimeZone));
$newLocalDate = $UTCDateTime->format('Y-m-d H:i:s');
echo $newLocalDate;