-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Render timezone listbox dynamically #1672
Merged
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d1ca62c
Render timezone listbox dynamically
asbiin a9966ea
Apply fixes from StyleCI
asbiin 1815e6b
Fixes and test
asbiin c251dcd
Apply fixes from StyleCI
asbiin 69ff4e1
Remove html render in code
asbiin 2937fe0
Apply fixes from StyleCI
asbiin b8d2aaa
Change names
asbiin bfa7aa9
Apply fixes from StyleCI
asbiin 1b329cd
Fix replace
asbiin 5c0b518
Merge branch 'feat/timezonelist' of github.com:monicahq/monica into f…
asbiin f2cc863
Fixes
asbiin a5da7c3
Apply fixes from StyleCI
asbiin 4c10760
Fix name
asbiin e53b4fc
Change disposition
asbiin 54b0f6e
Merge branch 'master' into feat/timezonelist
asbiin b3da97a
Merge branch 'master' into feat/timezonelist
asbiin 9247f4a
Update changelog
asbiin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Render timezone listbox dynamically
- Loading branch information
commit d1ca62c4a01ae8340f0644f9bad053583ab3adf3
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
use DateTimeZone; | ||
|
||
class TimezoneHelper | ||
{ | ||
/** | ||
* Get a list of all timezones | ||
* | ||
* @return array | ||
*/ | ||
public static function listTimezones() : array | ||
{ | ||
$list = []; | ||
$timezones = DateTimeZone::listIdentifiers(); | ||
|
||
foreach ($timezones as $timezone) { | ||
list($tz, $name) = self::formatTimezone($timezone); | ||
array_push($list, [ | ||
'id' => $tz, | ||
'timezone' => $timezone, | ||
'name' => $name | ||
]); | ||
} | ||
|
||
return array_values(array_sort($list, function ($value) { | ||
return $value['id']; | ||
})); | ||
} | ||
|
||
private static function formatTimezone($timezone) : array | ||
{ | ||
$time = now($timezone); | ||
|
||
$offset = $time->format('P'); | ||
|
||
if ($timezone == 'UTC') { | ||
$formatted = '(UTC) Universal Time Coordinated'; | ||
} else { | ||
$i = strstr($time->tzName, '/'); | ||
if ($i > 0) { | ||
$timezone = substr(strstr($time->tzName, '/'), 1); | ||
} | ||
$timezone = str_replace('St_', 'St. ', $timezone); | ||
$timezone = str_replace('_', ' ', $timezone); | ||
|
||
$formatted = '(UTC ' . $offset . ') ' . $timezone; | ||
} | ||
|
||
$tz = str_replace(':', '', $offset); | ||
$tz = intval($tz); | ||
|
||
return [$tz, $formatted]; | ||
} | ||
|
||
/** | ||
* Render listbox of timezones. | ||
* | ||
* @param string $name | ||
* @param string $selected | ||
* @param mixed $attr | ||
* @return string | ||
**/ | ||
public static function list($name, $selected='', $attr='') : string | ||
{ | ||
// Attributes for select element | ||
$attrSet = null; | ||
if (!empty($attr)) { | ||
if (is_array($attr)) { | ||
foreach ($attr as $attr_name => $attr_value) { | ||
$attrSet .= ' ' .$attr_name. '="' .$attr_value. '"'; | ||
} | ||
} else { | ||
$attrSet = ' ' .$attr; | ||
} | ||
} | ||
|
||
$listbox = '<select name="' .$name. '"' .$attrSet. '>'; | ||
|
||
$list = self::listTimezones(); | ||
|
||
foreach ($list as $key => $timezone) { | ||
$selected_attr = ($selected == $timezone['timezone']) ? ' selected="selected"' : ''; | ||
|
||
$listbox .= '<option value="' .$timezone['timezone']. '"' .$selected_attr. '>'; | ||
$listbox .= $timezone['name']; | ||
$listbox .= '</option>'; | ||
} | ||
|
||
$listbox .= '</select>'; | ||
|
||
return $listbox; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Description of this method?