0

I am trying to solve this exercise in PHP from a Python book

Write a program that converts a time from one time zone to another. The user enters the time in the usual American way, such as 3:48pm or 11:26am. The first time zone the user enters is that of the original time and the second is the desired time zone. The possible time zones are Eastern, Central, Mountain, or Pacific. Time: 11:48pm Starting zone: Pacific Ending zone: Eastern 2:48am

Struggling to come with a logic to solve it.

At the moment I am thinking about

IF AM
    Switch
       16 different cases
IF PM
    Switch
       16 different cases     

but it seems a stupid way of doing it.

Any ideas?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • https://www.php.net/manual/en/datetime.format.php – aynber Aug 24 '22 at 19:40
  • Write a function to convert from source time to UTC, and another from UTC to target time. PHP's [DateTime](https://www.php.net/manual/en/class.datetime.php) class can make this trivial. – Tangentially Perpendicular Aug 24 '22 at 19:44
  • Does this answer your question? [Timezone conversion in php](https://stackoverflow.com/questions/2505681/timezone-conversion-in-php) – Justinas Aug 24 '22 at 19:50
  • You don't need 16 different cases. You just need to know each zone's offset from UTC. Then you can calculate the difference in offsets, and adjust the time by that. – Barmar Aug 24 '22 at 19:54
  • Thanks! My brain is clearly not working! – Gabry Os Aug 24 '22 at 20:10

0 Answers0