How do i convert one time zone to another in php?

To convert from the given timezone to the desired timezone, we just have to add/subtract the difference of timezones [in SECONDS] to given timezone.

$my_timestamp = strtotime["2020-09-22 14:07:26"];
/*
 Convert timezones difference into seconds
 from -7:00 to +5:30  have 12hrs and 30min difference
 So in seconds, 12.5*60*60 is equaled to 45000 seconds
*/

$time_zone_difference = 45000;

//Use date function to get datetime in your desired formate
echo date["Y-m-d h:i:sa", $my_timestamp + time_zone_difference ];

or we can write it like Below given functions are for additional help.

Convert timezone differences in seconds, [which you can hardcode, if it is fixed throught the project]:

function timezoneDifferenceInSec[ $source_timezone, $required_timezone]{
    $a = explode[":",$source_timezone];
    $b = explode[":",$required_timezone];
    $c = [intval[$a[0]]*60+intval[$a[1]]]*60;
    $d = [intval[$b[0]]*60+intval[$b[1]]]*60;
    $diffsec =0;
    if[$c < $d]
        $diffsec = $d-$c;
    else
        $diffsec = $c-$d;
    return $diffsec;
    }

//function call
$differenc = timezoneDifferenceInSec["-07:00", "+05:30"];

Function to convert DateTime into required Timezone [if difference is known]:

 //datetime in String and timezone_differe is in int
function convertTimezone[ $source_date_time,  $timezone_diff_in_sec]{
    return date["Y-m-d h:i:sa", strtotime[$source_date_time] + $timezone_diff_in_sec];
 }

 //function call
 $timestamp = "2020-09-22 14:07:26";
 $timezone_difference = 4500; //ie from -07:00 to +05:30

 echo convertTimezone[ $timestamp, $timezone_difference];

It provides code examples to convert the date and time to a different timezone in PHP.

This tutorial provides code examples to convert the date and time value from one timezone to another timezone using DateTime and DateTimeZone classes. We can always convert the date and time value from the active timezone to a different timezone.

The below-mentioned examples show the conversion of the given date and time from UTC timezone to the Los Angeles timezone. It further converts the date and time from the Los Angeles timezone to the London timezone.

// Get Timezone - UTC
$utcTimezone = new DateTimeZone[ 'UTC' ];

// Set time
$time = new DateTime[ '2020-06-03 10:45:15', $utcTimezone ];

echo $time->format[ 'Y-m-d H:i:s' ]; // 2020-06-03 10:45:15

// Get Timezone - Los Angeles
$laTimezone = new DateTimeZone[ 'America/Los_Angeles' ];

$time->setTimeZone[ $laTimezone ];

// Convert UTC to Los Angeles
echo $time->format[ 'Y-m-d H:i:s' ]; // 2020-06-03 03:45:15

// Get Timezone - London
$loTimezone = new DateTimeZone[ 'Europe/London' ];

$time->setTimeZone[ $loTimezone ];

// Convert Los Angeles to London
echo $time->format[ 'Y-m-d H:i:s' ]; // 2020-06-03 11:45:15

We can do multiple timezone conversions as shown above to show multiple clocks, keeping UTC as the standard timezone.

We can also use the function date_default_timezone_set to set the default timezone and use a different timezone to convert the date and time from the default timezone to the given timezone as shown below.

Chủ Đề