How to check is timezone identifier valid from code?

后端 未结 8 1827
无人及你
无人及你 2020-12-05 04:53

I\'ll try to explain what\'s the problem here.

According to list of supported timezones from PHP manual, I can see all valid TZ identifiers in PHP.

My first

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 05:14

    Just an addendum to Cal's excellent answer. I think the following might be even faster...

    function isValidTimezoneID($tzid) {
        if (empty($tzid)) {
            return false;
        }
        foreach (timezone_abbreviations_list() as $zone) {
            foreach ($zone as $item) {
                if ($item["timezone_id"] == $tzid) {
                    return true;
                }
            }
        }
        return false;
    }
    

提交回复
热议问题