0

So I have in the DB a timestamp like this: 2016-03-31 21:10:15 this get's set in db to the server timezonedate_default_timezone_set("America/Los_Angeles");

Now when I list the data I need to convert this to a user timezone which I have stored somewhere else, is there a way to convert this and display it in a different timezone?

So if it was 5:00 PM for "America/Los_Angeles" I need to convert this to New_York for eg and show that time but for New_York timezone, I guess you know what I mean.

Uffo
  • 9,628
  • 24
  • 90
  • 154
  • Do yourself a favor for future projects and store literally everything as UTC, and only format for a specific timezone when displaying. It's a little more work, but having a consistent time format will do wonders for your sanity. – Sammitch Apr 15 '16 at 17:30

1 Answers1

2

Use DateTime objects, and you can play around with timezones to your heart's content:

$dateTime = '2016-03-31 21:10:15';
$originalTimezone = 'America/Los_Angeles';
$newTimezone = 'America/New_York';

$dto = new DateTime($dateTime, new DateTimeZone($originalTimezone));
$dto->setTimezone(new DateTimeZone($newTimezone));

echo $dto->format('Y-m-d H:i:s');
Mark Baker
  • 209,507
  • 32
  • 346
  • 385