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.

// Globally define the Timezone
define( 'TIMEZONE', 'UTC' );

// Set Timezone
date_default_timezone_set( TIMEZONE );

// Set time
$time = new DateTime( '2020-06-03 10:45:15' );

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

This is the easiest way to convert the time from one clock to another using the DateTime and DateTimeZone classes.

PHP has very powerful date handling functions and it makes TimeZone Conversion extremely simple.

In some PHP application where we need to display date and times based on the user time Zone, to achieve this behavior we will convert and then store Date and time in the GMT format, Based on this GMT value we can again Convert date and time to Target TimeZone. actually here you no need to convert GMT format to use the method , i am converting Date time to GMT because i am storing all uses dateTimes in GMT format, its merely my preference.

I have written a function which converts one timezone into another with out fail, it is simple use and you can test it easily.

function timeZoneConvert($fromTime, $fromTimezone, $toTimezone,$format = 'Y-m-d H:i:s') {
     // create timeZone object , with fromtimeZone
    $from = new DateTimeZone($fromTimezone);
	 // create timeZone object , with totimeZone
    $to = new DateTimeZone($toTimezone);
    // read give time into ,fromtimeZone
    $orgTime = new DateTime($fromTime, $from);
	// fromte input date to ISO 8601 date (added in PHP 5). the create new date time object
    $toTime = new DateTime($orgTime->format("c"));
	// set target time zone to $toTme ojbect.
    $toTime->setTimezone($to);
	// return reuslt.
    return $toTime->format($format);
}

How to use?

Just call the function pass the parameters as required, first 3 are compulsory required, last one is optional.

      echo timeZoneConvert(date('Y-m-d H:i:s'), 'CDT', 'GMT');
 
      echo timeZoneConvert('2000-01-01 00:00:00', 'CDT', 'GMT');

by using this method you can convert to any PHP supported timeZones and function will return merely vast result.

Reference Links :

http://php.net/manual/en/class.datetime.php

Post navigation

How do you change from one time zone to another?

Changing Timezones of ZonedDateTime To convert a ZonedDateTime instance from one timezone to another, follow the two steps: Create ZonedDateTime in 1st timezone. You may already have it in your application. Convert the first ZonedDateTime in second timezone using withZoneSameInstant() method.

What timezone is UTC for PHP?

The default timezone for PHP is UTC regardless of your server's timezone. This is the timezone used by all PHP date/time functions in your scripts. See PHP's list of supported timezones to find the names of all possible timezones you can use for the date.

What is timezone PHP?

Definition and Usage The date_default_timezone_set() function sets the default timezone used by all date/time functions in the script.

How do I convert between time zones in Java?

To convert any time to the specific timezone (for example: UTC -> local timezone and vise versa) with any time pattern you can use java. time library. This method will take time patterns (original and required format) and timezone (original time zone and required timezone) will give String as output.