This is a helper function that creates a select menu including timezones in human-readable format. Each underlying key is the php-friendly name for the timezone, making it easy to immediately store and use them. The array of timezones and their underlying keys were taken from the repository by tamaspap.
Recommended installation of this package is through composer. Include the following in your project's composer.json file:
Next, update Composer from the Terminal:
Once this operation completes, the final step is to add the service provider. Open config/app.php, and add a new item to the service providers array.
Now it's ready for use!
Timezone Convert Helper Functions
The package includes two helper functions 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.
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).
Select Form Builder
The package also includes a helper function for the creation of the select option. This is created by calling Timezone::selectForm().
The method selectForm accepts four optional parameters, string selected, placeholder, array formAttributes, and array optionAttributes. An example of it's use is shown below:
Note that the $selected parameter matches the string to the values on each option, not the display text. The paratmeters $formAttributes and $optionAttributes can be used for styling, roles, and generally anything you need. This is useful for applying Twitter Bootstrap styling.
Simply returning Timezone::selectForm() will output the following: