1

Can anyone let me know how to change UTC time from table column to IST(Indian Standard time) programatically with Datetime format?

I have currently following date time value available in UTC format and i need to convert it into Indian Standard Time in Magento2:

2018-09-18 11:13:07 AM (UTC)

it should convert to IST with following:

2018-09-18 04:43:07 PM (IST)

2
  • 1
    Hey You can reefer this solution it will work. Magento 2 Date time timezone Commented Sep 18, 2018 at 11:34
  • I have tried with following from your mentioned reference: $this->_localeDate->date(new \DateTime($historyDate))->format('m/d/y H:i:s'); but its not converting to IST, don't we need to set timezone anywhere like something? Commented Sep 18, 2018 at 11:52

2 Answers 2

4

Yes you can set timezone from admin panel.

Go to Store->configuration->Genral You will find time zone according to your store you can set timezone.

3
  • Yes correct but i want to convert my custom date into IST time with datetime format, actually order created date is in UTC timeformat and i want to show it with IST timezone also so that admin can know exact indian standard time with which order was placed. Commented Sep 18, 2018 at 12:02
  • If you want to do it in custom way then you can use PHP core time/date function.For exmple: $date = new DateTime('2012-07-16 01:00:00 +00'); $date->setTimezone(new DateTimeZone('Asia/Kolkata')); echo $date->format('Y-m-d H:i:s'); // 2012-07-15 05:00:00 This might help you!! Commented Sep 18, 2018 at 12:10
  • i have tried with above code: $date = new \DateTime($historyDate); $date->setTimezone(new \DateTimeZone('Asia/Kolkata')); return $date->format('Y-m-d H:i:s'); but its not converting to IST as i need Commented Sep 18, 2018 at 12:21
0

Finally i got solution below:

$date = new \DateTime($historyDate.' +00'); 
$date->setTimezone(new \DateTimeZone('Asia/Kolkata')); 
return $date->format('Y-m-d H:i:s');

this will always convert UTC to IST time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.