None of these answers worked for me (I skipped trying code that was overly bulky in size). I also think it's weird to change the default timezone just for a single conversion.
Here is my solution:
function changeTimeZone($dateString, $timeZoneSource = null, $timeZoneTarget = null) { if (empty($timeZoneSource)) { $timeZoneSource = date_default_timezone_get(); } if (empty($timeZoneTarget)) { $timeZoneTarget = date_default_timezone_get(); } $dt = new DateTime($dateString, new DateTimeZone($timeZoneSource)); $dt->setTimezone(new DateTimeZone($timeZoneTarget)); return $dt->format("Y-m-d H:i:s"); }So, to convert to the server default, you would just pass one timezone:
changeTimeZone("2016-10-24 16:28", "Asia/Tokyo");To convert from the server default to the user, you would leave the 2nd parameter null or blank:
changeTimeZone("2016-10-24 16:28", "", "Asia/Tokyo");And to switch between 2 timezones unrelated to the default, you would provide 2 timezones:
changeTimeZone("2016-10-24 16:28", "America/New_York", "Asia/Tokyo");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.
<?php// 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.
date_timezone_set
(PHP 5 >= 5.2.0, PHP 7, PHP 8)
DateTime::setTimezone -- date_timezone_set — Sets the time zone for the DateTime object
Parameters
objectProcedural style only: A DateTime object returned by date_create(). The function modifies this object.
timezoneA DateTimeZone object representing the desired time zone.
Return Values
Returns the DateTime object for method chaining. The underlaying point-in-time is not changed when calling this method.
Examples
Example #1 DateTime::setTimeZone() example
Object-oriented style
<?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') . "\n";
?>
Procedural style
<?php
$date = date_create('2000-01-01', timezone_open('Pacific/Nauru'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";date_timezone_set($date, timezone_open('Pacific/Chatham'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
?>
The above examples will output:
2000-01-01 00:00:00+12:00 2000-01-01 01:45:00+13:45
See Also
- DateTimeImmutable::setTimezone() - Sets the time zone
- DateTime::getTimezone() - Return time zone relative to given DateTime
- DateTimeZone::__construct() - Creates new DateTimeZone object
There are no user contributed notes for this page.