0

I have this following script:

$data = array(
  'fromTimezone'          => 'Asia/Kolkata',
  'toTimezone'            => 'America/New_York',
  'dateTime'              => '2020-02-06 08:34:11',
  'dateTimeFormat'        => 'Y-m-d H:i:s'
);

$fromTimezone           = $data['fromTimezone'];
$toTimezone             = $data['toTimezone'];
$dateTime               = $data['dateTime'];
$dateTimeFormat         = $data['dateTimeFormat'];


$fromZoneDateTime    = new DateTime($dateTime, new DateTimeZone($fromTimezone));

// synchronizing with the to-Timezone
$fromZoneDateTime->setTimezone(new DateTimeZone($toTimezone));

$returnDateTime    = date($dateTimeFormat, strtotime($fromZoneDateTime->format('Y-m-d H:i:s')));
echo $returnDateTime;

This is giving me 2020-02-05 22:04:11

But when I am checking the time in Real Time Application for Timezone conversion. like The Time Zone Converter, I am getting data as 2020-02-05 23:04:11.

enter image description here

Why is this mismatch? How can I overcome this?

Saswat
  • 12,320
  • 16
  • 77
  • 156

2 Answers2

1

The site you referenced is showing you the difference between your local time zone (presumably India) and the time in New York for the current date.

There are other sites that you can use to validate your assumptions, such as timeanddate.com, which shows the correct results for the date in question, which align to your code output.

screenshot

As for why, understand that while India is fixed to UTC+05:30 (IST), New York alternates between UTC-05:00 (EST) and UTC-04:00 (EDT). For more on this, please read the timezone tag wiki, especially the section "Time Zone != Offset". You might also want to review the DST tag wiki.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
0

I think that is because of the day light savings.

Please refer PHP daylight saving time detection

Ajanyan Pradeep
  • 1,097
  • 1
  • 16
  • 26