Zak
Zak

Reputation: 721

Get time in other cities given a city to start

Suppose I have a given city (eg. Milan) with a given time (which I write), and I need to know the time in other city (suppose New York and Tokyo), how can I accomplish this in code (the cities will be always the same, so... an array)?

Upvotes: 1

Views: 613

Answers (1)

deceze
deceze

Reputation: 522529

$ts = new DateTime('2016-11-17 11:39:00', new DateTimeZone('Europe/London'));
$ts->setTimezone(new DateTimeZone('America/New_York'));

echo $ts->format('Y-m-d H:i:s');

That's all you need to do. You just need to make sure you're using one of the defined timezones. If you have any cities not in this list, you need to map them to the appropriate timezone they're in, like Milan → Europe/Rome.

Upvotes: 4

Related Questions