定制 jessedp/php-timezones 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jessedp/php-timezones

Composer 安装命令:

composer require jessedp/php-timezones

包简介

Basic Timezone utilities aimed at Laravel

README 文档

README

Latest Version MIT Licensed Test Status Quality Score Total Downloads

A wrapper to enumerate PHP 5.6+, 7.x, 8.x timezones in a simplified way for use in various ways.

This is done with Laravel 5.5+ in mind - YMMV elsewhere.

Basics

  • Creates timezone arrays based on PHP's supported timezones with optional grouping by region
  • Lists are sorted by offset from high (+14:00) to low (-11:00)
  • Optionally group the arrays (multi-dim associated array) by region
    • sorting is the same, but only inside each region
  • For either case, return those as:
    • php arrays for whatever use your heart desires
    • HTML select list
  • 2 utility functions for converting to/from UTC

Installation

You can install this package using Composer.

$ composer require jessedp/php-timezones

Using version ^0.2.0 for jessedp/php-timezones
./composer.json has been updated
    ...

Usage

1. Render a timezone HTML Select list

The method Timezones::create() has three parameters:

Timezones::create($name, $selected, $opts);
  • $name required - the name of the select element
  • $selected - sets the selected value of list box, assuming the a value with the option exists
  • $opts an array of options as key=>value:
    • attr => array of key=>value pairs to be included in the select element (ie, 'id', 'class', etc.)
    • withregions => _boolean whether or not to use option groups for the regions/continents (defaults to false)
    • regions => array (of strings) specifying the region(s) to include

Basic Example

Timezones::create('timezone');

Returns a string similar to:

<select name="timezone">
  ...
  <option value="Africa/Abidjan">(GMT/UTC + 00:00) Abidjan</option>
  <option value="Africa/Accra">(GMT/UTC + 00:00) Accra</option>
  ...
</select>

"Selected" Example

Same as above, but Asia/Ho_Chi_Minh will be selected by default

Timezones::create('timezone', 'Asia/Ho_Chi_Minh');

"Options" Example

You may also add multiple attributes with an array.

Timezones::create('timezone', null,
            ['attr'=>[
             'id'    => 'my_id',
             'class' => 'form-control'
            ]
        ]);

Which gives us:

<select name="timezone" id="my_id" class="form-control">
  <option value="Pacific/Apia">
    (GMT/UTC + 14:00)&nbsp;&nbsp;&nbsp;&nbsp;Pacific/Apia
  </option>
  <option value="Pacific/Kiritimati">
    (GMT/UTC + 14:00)&nbsp;&nbsp;&nbsp;&nbsp;Pacific/Kiritimati
  </option>
  ...
  <option value="Asia/Shanghai">
    (GMT/UTC + 08:00)&nbsp;&nbsp;&nbsp;&nbsp;Asia/Shanghai
  </option>
  <option value="Asia/Singapore">
    (GMT/UTC + 08:00)&nbsp;&nbsp;&nbsp;&nbsp;Asia/Singapore
  </option>
  <option value="Asia/Taipei">
    (GMT/UTC + 08:00)&nbsp;&nbsp;&nbsp;&nbsp;Asia/Taipei
  </option>
  ...
  <option value="America/New_York">
    (GMT/UTC − 05:00)&nbsp;&nbsp;&nbsp;&nbsp;America/New York
  </option>
  ...
</select>

"Regions/Grouping" Example

Say you want the option groups but only a couple regions...

Timezones::create('timezone',null,
                    ['attr'=>['class'=>'form-control'],
                    'with_regions'=>true,
                    'regions'=>['Africa','America']
                    ])

This will return a string similar to the following:

<select name="timezone" class="form-control">
  <optgroup label="Africa">
    <option value="Africa/Addis_Ababa">
      (GMT/UTC + 03:00)&nbsp;&nbsp;&nbsp;&nbsp;Addis Ababa
    </option>
    <option value="Africa/Asmara">
      (GMT/UTC + 03:00)&nbsp;&nbsp;&nbsp;&nbsp;Asmara
    </option>
    ...
  </optgroup>
  <optgroup label="America">
    ...
    <option value="America/Noronha">
      (GMT/UTC − 02:00)&nbsp;&nbsp;&nbsp;&nbsp;Noronha
    </option>
    ...
    <option value="America/Argentina/Buenos_Aires">
      (GMT/UTC − 03:00)&nbsp;&nbsp;&nbsp;&nbsp;Argentina/Buenos Aires
    </option>
    ...
    <option value="America/New_York">
      (GMT/UTC − 05:00)&nbsp;&nbsp;&nbsp;&nbsp;New York
    </option>
    ...
  </optgroup>
</select>

2. Render a timezone array

You can also render timezone list as an array. To do so, just use the Timezones::toArray() method.

Example in Laravel:

$timezone_list = Timezones::toArray();

3. Utility methods

The package includes two methods that make it easy to deal with displaying and storing timezones, convertFromUTC() and convertToUTC() :

Each function accepts two required parameters and a third optional parameter dealing with the format of the returned timestamp.

    Timezones::convertFromUTC($timestamp, $timezone, $format);
    Timezones::convertToUTC($timestamp, $timezone, $format);

The first parameter accepts a timestamp, the second accepts the name of the timezone that you are converting to/from. The option values associated with the timezones included in the select form builder can be plugged into here as is. Alternatively, you can use any of PHP's supported timezones.

The third parameter is optional, and default is set to 'Y-m-d H:i:s' , which is how Laravel natively stores datetimes into the database (the created_at and updated_at columns). If you're using this for display purposes, you may find including '(e)' in the format string which displays the timezone.

Thanks to

This is based off some lovely work by:

The Spatie group and specifically the laravel-analytics project I used as inspiration copied for structure

jessedp/php-timezones 适用场景与选型建议

jessedp/php-timezones 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27.61k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 02 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「timezone」 「timezones」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jessedp/php-timezones 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jessedp/php-timezones 我们能提供哪些服务?
定制开发 / 二次开发

基于 jessedp/php-timezones 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 27.61k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-02-18