Convert date to est timezone php

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_setSets the time zone for the DateTime object

Parameters

object

Procedural style only: A DateTime object returned by date_create(). The function modifies this object.

timezone

A 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($datetimezone_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.

How to change date 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 date_ default_ timezone_ set in PHP?

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

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 is the default timezone in PHP?

Reading the timezone set using the date_default_timezone_set() function (if any) Reading the value of the date. timezone ini option (if set) If none of the above succeed, the default timezone will be UTC.