Get the official American time zone in PHP?

I have account stored in my database and I have each account state on file, from there I want to link each account to a timezone based on the account state.

so if the account’s state is California then the timezone will be “America/Los_Angeles”

More, I want to break down the time zone that is provided at this page http://php.net/manual/en/timezones.america.php by
Atlantic
Eastern
Central
Mountain
Pacific
Alaska
Hawaii – Aleutian

Now I will be able to sort my data based on 7 time zones and also I can have all my account linked to a timezone so I can determine their time zone.

So My question
1) How can I figure out what state is linked to what timezone. (example: California = “America/Los_Angeles”)

2) Which timezone are linked to what timezone category. (example: California = Pacific)

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You haven’t thought this through.

Many US states have multiple time zones. For example, South Dakota has both Mountain and Central time zones.

South Dakota Time Zone Map

If you desire to resolve a location to a time zone, you will need a much more granular location. Ideally, a latitude and longitude. If you don’t have one, you can approximate the centroid lat/lon of a zip code, and then use that against any of many various services or databases that will resolve that to a time zone. But be very careful, not all zip codes represent physical locations, and zip codes change frequently.

Solution 2

It’s always best to go to the source:
http://www.php.net/manual/en/timezones.america.php

For all timezones:
http://www.php.net/manual/en/timezones.php

If you read down the page on the first link, someone was nice enough to make an array if you need the abbreviations:

$aTimeZones = array(
  'America/Puerto_Rico'=>'AST',
  'America/New_York'=>'EDT',
  'America/Chicago'=>'CDT',
  'America/Boise'=>'MDT',
  'America/Phoenix'=>'MST',
  'America/Los_Angeles'=>'PDT',
  'America/Juneau'=>'AKDT',
  'Pacific/Honolulu'=>'HST',
  'Pacific/Guam'=>'ChST',
  'Pacific/Samoa'=>'SST',
  'Pacific/Wake'=>'WAKT',
); 

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply