How to convert date to another timezone 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.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

// now
$date = date_create('2010-12-31 22:00:01', timezone_open('Europe/Amsterdam'));
// Buenos Aires
date_timezone_set($date, timezone_open('America/Argentina/Buenos_Aires'));
echo $date->format('Y-m-d H:i:s');
// 2010-12-31 18:00:01
// UTC
date_timezone_set($date, timezone_open('UTC'));
echo $date->format('Y-m-d H:i:s');
// 2010-12-31 20:00:01
// London
date_timezone_set($date, timezone_open('Europe/London'));
echo $date->format('Y-m-d H:i:s');
// 2010-12-31 21:00:01
// Maldives
date_timezone_set($date, timezone_open('Indian/Maldives'));
echo $date->format('Y-m-d H:i:s');
// 2011-01-01 02:00:01
// Melbourne
date_timezone_set($date, timezone_open('Australia/Melbourne'));
echo $date->format('Y-m-d H:i:s');
// 2011-01-01 08:00:01

How to convert Date and time from one time zone to another in php?

php $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru')); echo $date->format('Y-m-d H:i:sP') . "\n"; $date->setTimezone(new DateTimeZone('Pacific/Chatham')); echo $date->format('Y-m-d H:i:sP') .

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.

How do I change the default timezone in PHP?

8 Answers.
Go to your phpinfo() page and search for Loaded Configuration File and open the php. ini file mentioned under that section..
Change the default timezone settings by adding your new timezone by modifying this line: date. timezone=Asia/Kolkata ..
Save the php. ... .
Restart the Apache server..

What is the default timezone in PHP?

The date_default_timezone_get() function returns the default timezone used by all date/time functions in the script.