Krishna_ramisetty Posted May 6, 2021 Share Posted May 6, 2021 Hello Everyone, I have below datetime string 2021-05-06T13:48:19.2064951+05:30 i need to get the server timezone name by passing this string to date_default_timezone_set(). here is it valid? can somebody please help me i need the timezone name with above datetime string Thanks, Krish Quote Link to comment https://forums.phpfreaks.com/topic/312621-how-to-get-timezone-name-by-passing-datetime-string-to-date_default_timezone_set/ Share on other sites More sharing options...
gw1500se Posted May 6, 2021 Share Posted May 6, 2021 This should help. Quote Link to comment https://forums.phpfreaks.com/topic/312621-how-to-get-timezone-name-by-passing-datetime-string-to-date_default_timezone_set/#findComment-1586359 Share on other sites More sharing options...
maxxd Posted May 6, 2021 Share Posted May 6, 2021 Or this. Quote Link to comment https://forums.phpfreaks.com/topic/312621-how-to-get-timezone-name-by-passing-datetime-string-to-date_default_timezone_set/#findComment-1586360 Share on other sites More sharing options...
Krishna_ramisetty Posted May 6, 2021 Author Share Posted May 6, 2021 45 minutes ago, gw1500se said: This should help. Not getting exactly what's happening here as I am new to PHP. any demonstration will be helpful a lot for me Quote Link to comment https://forums.phpfreaks.com/topic/312621-how-to-get-timezone-name-by-passing-datetime-string-to-date_default_timezone_set/#findComment-1586361 Share on other sites More sharing options...
gw1500se Posted May 6, 2021 Share Posted May 6, 2021 (edited) There is an example in the manual. You simply create a datetime object using your date string then use the timezone function to get what you need. Edited May 6, 2021 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/312621-how-to-get-timezone-name-by-passing-datetime-string-to-date_default_timezone_set/#findComment-1586362 Share on other sites More sharing options...
Barand Posted May 6, 2021 Share Posted May 6, 2021 One problem you will have is that for any offset there are usually several possible names. For your "+05:30" example there are only two but as the array below shows, there are usualy quite a few more ... $timezones = array ( '+00:00' => array ( 0 => 'Africa/Abidjan', 1 => 'Africa/Accra', 2 => 'Africa/Bamako', 3 => 'Africa/Banjul', 4 => 'Africa/Bissau', 5 => 'Africa/Casablanca', 6 => 'Africa/Conakry', 7 => 'Africa/Dakar', 8 => 'Africa/El_Aaiun', 9 => 'Africa/Freetown', 10 => 'Africa/Lome', 11 => 'Africa/Monrovia', 12 => 'Africa/Nouakchott', 13 => 'Africa/Ouagadougou', 14 => 'Africa/Sao_Tome', 15 => 'America/Danmarkshavn', 16 => 'America/Scoresbysund', 17 => 'Atlantic/Azores', 18 => 'Atlantic/Reykjavik', 19 => 'Atlantic/St_Helena', 20 => 'UTC', ), '+01:00' => array ( 0 => 'Africa/Algiers', 1 => 'Africa/Bangui', 2 => 'Africa/Brazzaville', 3 => 'Africa/Douala', 4 => 'Africa/Kinshasa', 5 => 'Africa/Lagos', 6 => 'Africa/Libreville', 7 => 'Africa/Luanda', 8 => 'Africa/Malabo', 9 => 'Africa/Ndjamena', 10 => 'Africa/Niamey', 11 => 'Africa/Porto-Novo', 12 => 'Africa/Tunis', 13 => 'Atlantic/Canary', 14 => 'Atlantic/Faroe', 15 => 'Atlantic/Madeira', 16 => 'Europe/Dublin', 17 => 'Europe/Guernsey', 18 => 'Europe/Isle_of_Man', 19 => 'Europe/Jersey', 20 => 'Europe/Lisbon', 21 => 'Europe/London', ), '+02:00' => array ( 0 => 'Africa/Blantyre', 1 => 'Africa/Bujumbura', 2 => 'Africa/Cairo', 3 => 'Africa/Ceuta', 4 => 'Africa/Gaborone', 5 => 'Africa/Harare', 6 => 'Africa/Johannesburg', 7 => 'Africa/Khartoum', 8 => 'Africa/Kigali', 9 => 'Africa/Lubumbashi', 10 => 'Africa/Lusaka', 11 => 'Africa/Maputo', 12 => 'Africa/Maseru', 13 => 'Africa/Mbabane', 14 => 'Africa/Tripoli', 15 => 'Africa/Windhoek', 16 => 'Antarctica/Troll', 17 => 'Arctic/Longyearbyen', 18 => 'Europe/Amsterdam', 19 => 'Europe/Andorra', 20 => 'Europe/Belgrade', 21 => 'Europe/Berlin', 22 => 'Europe/Bratislava', 23 => 'Europe/Brussels', 24 => 'Europe/Budapest', 25 => 'Europe/Busingen', 26 => 'Europe/Copenhagen', 27 => 'Europe/Gibraltar', 28 => 'Europe/Kaliningrad', 29 => 'Europe/Ljubljana', 30 => 'Europe/Luxembourg', 31 => 'Europe/Madrid', 32 => 'Europe/Malta', 33 => 'Europe/Monaco', 34 => 'Europe/Oslo', 35 => 'Europe/Paris', 36 => 'Europe/Podgorica', 37 => 'Europe/Prague', 38 => 'Europe/Rome', 39 => 'Europe/San_Marino', 40 => 'Europe/Sarajevo', 41 => 'Europe/Skopje', 42 => 'Europe/Stockholm', 43 => 'Europe/Tirane', 44 => 'Europe/Vaduz', 45 => 'Europe/Vatican', 46 => 'Europe/Vienna', 47 => 'Europe/Warsaw', 48 => 'Europe/Zagreb', 49 => 'Europe/Zurich', ), '+03:00' => array ( 0 => 'Africa/Addis_Ababa', 1 => 'Africa/Asmara', 2 => 'Africa/Dar_es_Salaam', 3 => 'Africa/Djibouti', 4 => 'Africa/Juba', 5 => 'Africa/Kampala', 6 => 'Africa/Mogadishu', 7 => 'Africa/Nairobi', 8 => 'Antarctica/Syowa', 9 => 'Asia/Aden', 10 => 'Asia/Amman', 11 => 'Asia/Baghdad', 12 => 'Asia/Bahrain', 13 => 'Asia/Beirut', 14 => 'Asia/Damascus', 15 => 'Asia/Famagusta', 16 => 'Asia/Gaza', 17 => 'Asia/Hebron', 18 => 'Asia/Jerusalem', 19 => 'Asia/Kuwait', 20 => 'Asia/Nicosia', 21 => 'Asia/Qatar', 22 => 'Asia/Riyadh', 23 => 'Europe/Athens', 24 => 'Europe/Bucharest', 25 => 'Europe/Chisinau', 26 => 'Europe/Helsinki', 27 => 'Europe/Istanbul', 28 => 'Europe/Kiev', 29 => 'Europe/Kirov', 30 => 'Europe/Mariehamn', 31 => 'Europe/Minsk', 32 => 'Europe/Moscow', 33 => 'Europe/Riga', 34 => 'Europe/Simferopol', 35 => 'Europe/Sofia', 36 => 'Europe/Tallinn', 37 => 'Europe/Uzhgorod', 38 => 'Europe/Vilnius', 39 => 'Europe/Volgograd', 40 => 'Europe/Zaporozhye', 41 => 'Indian/Antananarivo', 42 => 'Indian/Comoro', 43 => 'Indian/Mayotte', ), '+04:00' => array ( 0 => 'Asia/Baku', 1 => 'Asia/Dubai', 2 => 'Asia/Muscat', 3 => 'Asia/Tbilisi', 4 => 'Asia/Yerevan', 5 => 'Europe/Astrakhan', 6 => 'Europe/Samara', 7 => 'Europe/Saratov', 8 => 'Europe/Ulyanovsk', 9 => 'Indian/Mahe', 10 => 'Indian/Mauritius', 11 => 'Indian/Reunion', ), '+04:30' => array ( 0 => 'Asia/Kabul', 1 => 'Asia/Tehran', ), '+05:00' => array ( 0 => 'Antarctica/Mawson', 1 => 'Asia/Aqtau', 2 => 'Asia/Aqtobe', 3 => 'Asia/Ashgabat', 4 => 'Asia/Atyrau', 5 => 'Asia/Dushanbe', 6 => 'Asia/Karachi', 7 => 'Asia/Oral', 8 => 'Asia/Samarkand', 9 => 'Asia/Tashkent', 10 => 'Asia/Yekaterinburg', 11 => 'Indian/Kerguelen', 12 => 'Indian/Maldives', ), '+05:30' => array ( 0 => 'Asia/Colombo', 1 => 'Asia/Kolkata', ), '+05:45' => array ( 0 => 'Asia/Kathmandu', ), '+06:00' => array ( 0 => 'Antarctica/Vostok', 1 => 'Asia/Almaty', 2 => 'Asia/Bishkek', 3 => 'Asia/Dhaka', 4 => 'Asia/Omsk', 5 => 'Asia/Qyzylorda', 6 => 'Asia/Thimphu', 7 => 'Asia/Urumqi', 8 => 'Indian/Chagos', ), '+06:30' => array ( 0 => 'Asia/Yangon', 1 => 'Indian/Cocos', ), '+07:00' => array ( 0 => 'Antarctica/Davis', 1 => 'Asia/Bangkok', 2 => 'Asia/Barnaul', 3 => 'Asia/Ho_Chi_Minh', 4 => 'Asia/Hovd', 5 => 'Asia/Jakarta', 6 => 'Asia/Krasnoyarsk', 7 => 'Asia/Novokuznetsk', 8 => 'Asia/Novosibirsk', 9 => 'Asia/Phnom_Penh', 10 => 'Asia/Pontianak', 11 => 'Asia/Tomsk', 12 => 'Asia/Vientiane', 13 => 'Indian/Christmas', ), '+08:00' => array ( 0 => 'Asia/Brunei', 1 => 'Asia/Choibalsan', 2 => 'Asia/Hong_Kong', 3 => 'Asia/Irkutsk', 4 => 'Asia/Kuala_Lumpur', 5 => 'Asia/Kuching', 6 => 'Asia/Macau', 7 => 'Asia/Makassar', 8 => 'Asia/Manila', 9 => 'Asia/Shanghai', 10 => 'Asia/Singapore', 11 => 'Asia/Taipei', 12 => 'Asia/Ulaanbaatar', 13 => 'Australia/Perth', ), '+08:30' => array ( 0 => 'Asia/Pyongyang', ), '+08:45' => array ( 0 => 'Australia/Eucla', ), '+09:00' => array ( 0 => 'Asia/Chita', 1 => 'Asia/Dili', 2 => 'Asia/Jayapura', 3 => 'Asia/Khandyga', 4 => 'Asia/Seoul', 5 => 'Asia/Tokyo', 6 => 'Asia/Yakutsk', 7 => 'Pacific/Palau', ), '+09:30' => array ( 0 => 'Australia/Adelaide', 1 => 'Australia/Broken_Hill', 2 => 'Australia/Darwin', ), '+10:00' => array ( 0 => 'Antarctica/DumontDUrville', 1 => 'Asia/Ust-Nera', 2 => 'Asia/Vladivostok', 3 => 'Australia/Brisbane', 4 => 'Australia/Currie', 5 => 'Australia/Hobart', 6 => 'Australia/Lindeman', 7 => 'Australia/Melbourne', 8 => 'Australia/Sydney', 9 => 'Pacific/Chuuk', 10 => 'Pacific/Guam', 11 => 'Pacific/Port_Moresby', 12 => 'Pacific/Saipan', ), '+10:30' => array ( 0 => 'Australia/Lord_Howe', ), '+11:00' => array ( 0 => 'Antarctica/Casey', 1 => 'Antarctica/Macquarie', 2 => 'Asia/Magadan', 3 => 'Asia/Sakhalin', 4 => 'Asia/Srednekolymsk', 5 => 'Pacific/Bougainville', 6 => 'Pacific/Efate', 7 => 'Pacific/Guadalcanal', 8 => 'Pacific/Kosrae', 9 => 'Pacific/Norfolk', 10 => 'Pacific/Noumea', 11 => 'Pacific/Pohnpei', ), '+12:00' => array ( 0 => 'Antarctica/McMurdo', 1 => 'Asia/Anadyr', 2 => 'Asia/Kamchatka', 3 => 'Pacific/Auckland', 4 => 'Pacific/Fiji', 5 => 'Pacific/Funafuti', 6 => 'Pacific/Kwajalein', 7 => 'Pacific/Majuro', 8 => 'Pacific/Nauru', 9 => 'Pacific/Tarawa', 10 => 'Pacific/Wake', 11 => 'Pacific/Wallis', ), '+12:45' => array ( 0 => 'Pacific/Chatham', ), '+13:00' => array ( 0 => 'Pacific/Apia', 1 => 'Pacific/Enderbury', 2 => 'Pacific/Fakaofo', 3 => 'Pacific/Tongatapu', ), '+14:00' => array ( 0 => 'Pacific/Kiritimati', ), '-01:00' => array ( 0 => 'Atlantic/Cape_Verde', ), '-02:-30' => array ( 0 => 'America/St_Johns', ), '-02:00' => array ( 0 => 'America/Godthab', 1 => 'America/Miquelon', 2 => 'America/Noronha', 3 => 'Atlantic/South_Georgia', ), '-03:00' => array ( 0 => 'America/Araguaina', 1 => 'America/Argentina/Buenos_Aires', 2 => 'America/Argentina/Catamarca', 3 => 'America/Argentina/Cordoba', 4 => 'America/Argentina/Jujuy', 5 => 'America/Argentina/La_Rioja', 6 => 'America/Argentina/Mendoza', 7 => 'America/Argentina/Rio_Gallegos', 8 => 'America/Argentina/Salta', 9 => 'America/Argentina/San_Juan', 10 => 'America/Argentina/San_Luis', 11 => 'America/Argentina/Tucuman', 12 => 'America/Argentina/Ushuaia', 13 => 'America/Bahia', 14 => 'America/Belem', 15 => 'America/Cayenne', 16 => 'America/Fortaleza', 17 => 'America/Glace_Bay', 18 => 'America/Goose_Bay', 19 => 'America/Halifax', 20 => 'America/Maceio', 21 => 'America/Moncton', 22 => 'America/Montevideo', 23 => 'America/Paramaribo', 24 => 'America/Punta_Arenas', 25 => 'America/Recife', 26 => 'America/Santarem', 27 => 'America/Santiago', 28 => 'America/Sao_Paulo', 29 => 'America/Thule', 30 => 'Antarctica/Palmer', 31 => 'Antarctica/Rothera', 32 => 'Atlantic/Bermuda', 33 => 'Atlantic/Stanley', ), '-04:00' => array ( 0 => 'America/Anguilla', 1 => 'America/Antigua', 2 => 'America/Aruba', 3 => 'America/Asuncion', 4 => 'America/Barbados', 5 => 'America/Blanc-Sablon', 6 => 'America/Boa_Vista', 7 => 'America/Campo_Grande', 8 => 'America/Caracas', 9 => 'America/Cuiaba', 10 => 'America/Curacao', 11 => 'America/Detroit', 12 => 'America/Dominica', 13 => 'America/Grand_Turk', 14 => 'America/Grenada', 15 => 'America/Guadeloupe', 16 => 'America/Guyana', 17 => 'America/Havana', 18 => 'America/Indiana/Indianapolis', 19 => 'America/Indiana/Marengo', 20 => 'America/Indiana/Petersburg', 21 => 'America/Indiana/Vevay', 22 => 'America/Indiana/Vincennes', 23 => 'America/Indiana/Winamac', 24 => 'America/Iqaluit', 25 => 'America/Kentucky/Louisville', 26 => 'America/Kentucky/Monticello', 27 => 'America/Kralendijk', 28 => 'America/La_Paz', 29 => 'America/Lower_Princes', 30 => 'America/Manaus', 31 => 'America/Marigot', 32 => 'America/Martinique', 33 => 'America/Montserrat', 34 => 'America/Nassau', 35 => 'America/New_York', 36 => 'America/Nipigon', 37 => 'America/Pangnirtung', 38 => 'America/Port-au-Prince', 39 => 'America/Port_of_Spain', 40 => 'America/Porto_Velho', 41 => 'America/Puerto_Rico', 42 => 'America/Santo_Domingo', 43 => 'America/St_Barthelemy', 44 => 'America/St_Kitts', 45 => 'America/St_Lucia', 46 => 'America/St_Thomas', 47 => 'America/St_Vincent', 48 => 'America/Thunder_Bay', 49 => 'America/Toronto', 50 => 'America/Tortola', ), '-05:00' => array ( 0 => 'America/Atikokan', 1 => 'America/Bahia_Banderas', 2 => 'America/Bogota', 3 => 'America/Cancun', 4 => 'America/Cayman', 5 => 'America/Chicago', 6 => 'America/Eirunepe', 7 => 'America/Guayaquil', 8 => 'America/Indiana/Knox', 9 => 'America/Indiana/Tell_City', 10 => 'America/Jamaica', 11 => 'America/Lima', 12 => 'America/Matamoros', 13 => 'America/Menominee', 14 => 'America/Merida', 15 => 'America/Mexico_City', 16 => 'America/Monterrey', 17 => 'America/North_Dakota/Beulah', 18 => 'America/North_Dakota/Center', 19 => 'America/North_Dakota/New_Salem', 20 => 'America/Panama', 21 => 'America/Rainy_River', 22 => 'America/Rankin_Inlet', 23 => 'America/Resolute', 24 => 'America/Rio_Branco', 25 => 'America/Winnipeg', 26 => 'Pacific/Easter', ), '-06:00' => array ( 0 => 'America/Belize', 1 => 'America/Boise', 2 => 'America/Cambridge_Bay', 3 => 'America/Chihuahua', 4 => 'America/Costa_Rica', 5 => 'America/Denver', 6 => 'America/Edmonton', 7 => 'America/El_Salvador', 8 => 'America/Guatemala', 9 => 'America/Inuvik', 10 => 'America/Managua', 11 => 'America/Mazatlan', 12 => 'America/Ojinaga', 13 => 'America/Regina', 14 => 'America/Swift_Current', 15 => 'America/Tegucigalpa', 16 => 'America/Yellowknife', 17 => 'Pacific/Galapagos', ), '-07:00' => array ( 0 => 'America/Creston', 1 => 'America/Dawson', 2 => 'America/Dawson_Creek', 3 => 'America/Fort_Nelson', 4 => 'America/Hermosillo', 5 => 'America/Los_Angeles', 6 => 'America/Phoenix', 7 => 'America/Tijuana', 8 => 'America/Vancouver', 9 => 'America/Whitehorse', ), '-08:00' => array ( 0 => 'America/Anchorage', 1 => 'America/Juneau', 2 => 'America/Metlakatla', 3 => 'America/Nome', 4 => 'America/Sitka', 5 => 'America/Yakutat', 6 => 'Pacific/Pitcairn', ), '-09:-30' => array ( 0 => 'Pacific/Marquesas', ), '-09:00' => array ( 0 => 'America/Adak', 1 => 'Pacific/Gambier', ), '-10:00' => array ( 0 => 'Pacific/Honolulu', 1 => 'Pacific/Rarotonga', 2 => 'Pacific/Tahiti', ), '-11:00' => array ( 0 => 'Pacific/Midway', 1 => 'Pacific/Niue', 2 => 'Pacific/Pago_Pago', ), ); Quote Link to comment https://forums.phpfreaks.com/topic/312621-how-to-get-timezone-name-by-passing-datetime-string-to-date_default_timezone_set/#findComment-1586370 Share on other sites More sharing options...
maxxd Posted May 7, 2021 Share Posted May 7, 2021 2 hours ago, Barand said: One problem you will have is that for any offset there are usually several possible names. For your "+05:30" example there are only two but as the array below shows, there are usualy quite a few more .. Legit question - does https://www.php.net/manual/en/datetime.gettimezone.php not handle that? Quote Link to comment https://forums.phpfreaks.com/topic/312621-how-to-get-timezone-name-by-passing-datetime-string-to-date_default_timezone_set/#findComment-1586371 Share on other sites More sharing options...
Psycho Posted May 7, 2021 Share Posted May 7, 2021 (edited) 3 hours ago, maxxd said: Legit question - does https://www.php.net/manual/en/datetime.gettimezone.php not handle that? Look at the code examples on that page. The examples first create a datetime object with a specific timezone and then use gettimezone to retrieve the timezone that was previously set. A string such as "2021-05-06T13:48:19.2064951+05:30" has only the offset from UTC time - there is no timezone identifier. So, there is no way to programmatically determine the correct timezone out of the ones that share that offset. Edited May 7, 2021 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/312621-how-to-get-timezone-name-by-passing-datetime-string-to-date_default_timezone_set/#findComment-1586373 Share on other sites More sharing options...
Krishna_ramisetty Posted May 7, 2021 Author Share Posted May 7, 2021 4 hours ago, maxxd said: Legit question - does https://www.php.net/manual/en/datetime.gettimezone.php not handle that? Hi, here i am getting the time zone name as well. but in timezone_name_from_abbr() what is 19800, 0 and how to calculate it? $time = '2021-05-06T19:48:19.2064951+05:30'; $dt = new DateTime($time); error_log($dt->getTimezone()->getName()); error_log(timezone_name_from_abbr("", 19800, 0)); Quote Link to comment https://forums.phpfreaks.com/topic/312621-how-to-get-timezone-name-by-passing-datetime-string-to-date_default_timezone_set/#findComment-1586374 Share on other sites More sharing options...
Psycho Posted May 7, 2021 Share Posted May 7, 2021 (edited) 16 minutes ago, Krishna_ramisetty said: Hi, here i am getting the time zone name as well. but in timezone_name_from_abbr() what is 19800, 0 and how to calculate it? $time = '2021-05-06T19:48:19.2064951+05:30'; $dt = new DateTime($time); error_log($dt->getTimezone()->getName()); error_log(timezone_name_from_abbr("", 19800, 0)); 5.5 (i.e. 5:30) * 60 (minutes) * 60 (seconds) = 19800 I assume the 0 represent the first element of the array of timezone within the +5:30 UTC offset - 'Asia/Colombo'. Further, I expect you will ALWAYS get the first timezone corresponding to the UTC offset (as a default) because there is no way that an actual timezone can be specified in the string value you are using. '+05:30' => array ( 0 => 'Asia/Colombo', 1 => 'Asia/Kolkata', ), Edited May 7, 2021 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/312621-how-to-get-timezone-name-by-passing-datetime-string-to-date_default_timezone_set/#findComment-1586375 Share on other sites More sharing options...
maxxd Posted May 7, 2021 Share Posted May 7, 2021 7 hours ago, Psycho said: Look at the code examples on that page. The examples first create a datetime object with a specific timezone and then use gettimezone to retrieve the timezone that was previously set. A string such as "2021-05-06T13:48:19.2064951+05:30" has only the offset from UTC time - there is no timezone identifier. So, there is no way to programmatically determine the correct timezone out of the ones that share that offset. Gotcha - I didn't read everything correctly. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/312621-how-to-get-timezone-name-by-passing-datetime-string-to-date_default_timezone_set/#findComment-1586379 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.