2017 © Pedro Peláez
 

library php-timezones

Basic Timezone utilities aimed at Laravel

image

jessedp/php-timezones

Basic Timezone utilities aimed at Laravel

  • Saturday, July 7, 2018
  • by jessedp
  • Repository
  • 1 Watchers
  • 0 Stars
  • 52 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 13 % Grown

The README.md

php-timezones

Latest Version MIT Licensed Test Status Quality Score Total Downloads, (*1)

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

This is done with Laravel 5.5+ in mind - YMMV elsewhere., (*3)

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., (*4)

``` bash $ composer require jessedp/php-timezones, (*5)

Using version ^0.2.0 for jessedp/php-timezones ./composer.json has been updated ..., (*6)


## Usage ### 1. Render a timezone HTML Select list The method `Timezones::create()` has three parameters: ``` php 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.)
    • with_regions => 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

``` php Timezones::create('timezone');, (*7)


Returns a string similar to: ``` html <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, (*8)

``` php Timezones::create('timezone', 'Asia/Ho_Chi_Minh');, (*9)


#### "Options" Example You may also add multiple attributes with an array. ``` php Timezones::create('timezone', null, ['attr'=>[ 'id' => 'my_id', 'class' => 'form-control' ] ]);

Which gives us:, (*10)

``` html , (*11)


#### "Regions/Grouping" Example Say you want the option groups but only a couple regions... ``` php Timezones::create('timezone',null, ['attr'=>['class'=>'form-control'], 'with_regions'=>true, 'regions'=>['Africa','America'] ])

This will return a string similar to the following:, (*12)

``` html , (*13)


### 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: ``` php $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() :, (*14)

Each function accepts two required parameters and a third optional parameter dealing with the format of the returned timestamp., (*15)

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

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., (*17)

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., (*18)

Thanks to

This is based off some lovely work by:, (*19)

  • https://github.com/JackieDo/Timezone-List
  • https://github.com/camroncade/timezone

The Spatie group and specifically the laravel-analytics project I used ~~as inspiration~~ copied for structure, (*20)

The Versions

07/07 2018

dev-master

9999999-dev

Basic Timezone utilities aimed at Laravel

  Sources   Download

MIT

The Requires

 

by Avatar jessedp

laravel timezone timezones

22/02 2018

v0.1.1

0.1.1.0

Basic Timezone utilities aimed at Laravel

  Sources   Download

MIT

The Requires

 

by Avatar jessedp

laravel timezone timezones

20/02 2018

v0.1.0

0.1.0.0

Basic Timezone utilities aimed at Laravel

  Sources   Download

MIT

The Requires

 

by Avatar jessedp

laravel timezone timezones