jacobemerick/timezone-converter

0.1.0 2015-03-19 00:32 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:29:30 UTC


README

A simply and ugly little invokable class to convert different timezone formats into IANA-approved DateTimeZones. While PHP has some awesome parsing to handle many different datetime formats, it is not so forgiving about timezones. This class helps out with this.

用法

This currently supports converting between four different timezone formats. You can either define which timezone you are interested in or just throw an 'any' match at it, though that is a bit more expensive.

// basic instantiation
use Jacobemerick\TimezoneConverter\Converter;

$converter = new Converter(Converter::ABBREVIATION_FORMAT);
$timezone = $converter('est');

The $timezone variable is now a instance of DateTimeZone, with an internal setting of America/New_York.

UTC格式

UTC格式之间转换相当棘手,因为UTC偏移量不包含有关标准/夏令时的任何上下文信息。请谨慎使用此选项。

$converter = new Converter(Converter::UTC_FORMAT);
$timezone = $converter('-0500'); // America/New_York

军事格式

军事格式期望偏移量为硬返回,没有任何夏令时或地理异常的上下文,此选项返回Etc/GMT(offset)时区。您可以使用北约语音字母表或直接使用英文字母传递值。

$converter = new Converter(Converter::MILITARY_FORMAT);
$timezone = $converter('Romeo'); // Etc/GMT-5

缩写格式

时区缩写本身很难处理,因为它们不如IANA格式全面,并且为几个区域重复。响应可假设为最佳猜测转换。

$converter = new Converter(Converter::ABBREVIATION_FORMAT);
$timezone = $converter('est'); // America/New_York

Rails格式

有一个Rails gem尝试将默认的IANA格式列表“简化”为146个选项。这很好,直到你需要转换回来。

$converter = new Converter(Converter::RAILS_FORMAT);
$timezone = $converter('Eastern Time (US & Canada)'); // America/New_York

其他选项

为了完整性,您还可以传递IANA时区格式。您还可以传递“任何”查询,它将遍历选项并尝试找到一些东西。这可以被视为最后的努力。

$converter = new Converter(Converter::IANA_FORMAT);
$timezone = $converter('America/New_York'); // America/New_York

$converter = new Converter(Converter::ANY_FORMAT);
$timezone = $converter('est'); // America/New_York

安装

通过composer

$ composer require jacobemerick/timezone-converter:~0.1