Santanu Biswas
Santanu Biswas

Reputation: 41

Convert date and time from UTC to IST and vice versa in PHP

I am working on a project which saves the time in UTC zone. I want to display the time in Asia/Kolkata zone in the front end.

$createdAt = '2017-03-16 23:21:56'; 

/* The indian time was 2017-03-17 11:52 when the record was placed, so after converting it to Asia/Kolkata it should display 2017-03-17 11:52 (seconds does not matter) */

My PHP code to convert it into Asia/Kolkata

$createdAt = '2017-03-16 23:21:56';
$dateFrom = new DateTime($createdAt, new DateTimeZone('America/Chicago'));
$dateFrom->setTimezone(new DateTimeZone('Asia/Kolkata'));
$IST = $dateFrom->format('Y-m-d H:i:s');

The output is 2017-03-17 09:51:56 instead of 2017-03-17 11:52:56 . The output shows 2 hours before the actual time. I can't figure it out.

Your suggestions will help me a lot.

Upvotes: 0

Views: 2557

Answers (1)

ewcz
ewcz

Reputation: 13087

If your input date/time is in the Chicago time zone, then one should take into account the DST. The change occurred on 12th March 02:00 local time so your input (assuming $createdAt is correct) is already in DST regime (i.e., UTC -05:00).

Since Kolkata is UTC+05:30 (and there is no DST adjustment), the total difference for your particular input is 10:30 which justifies the output of 2017-03-17 09:51:56.

Upvotes: 1

Related Questions