Skip to content

Commit 5727be1

Browse files
authored
Update display format from lonnie
1 parent ec27b5c commit 5727be1

File tree

1 file changed

+107
-8
lines changed

1 file changed

+107
-8
lines changed

system/Helpers/date_helper.php

Lines changed: 107 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,76 @@
4141
* @package CodeIgniter
4242
*/
4343

44+
if (! function_exists('now'))
45+
{
46+
/**
47+
* Get "now" time
48+
*
49+
* Returns time() based on the timezone parameter or on the
50+
* app_timezone() setting
51+
*
52+
* @param string $timezone
53+
*
54+
* @return integer
55+
* @throws \Exception
56+
*/
57+
function now(string $timezone = null): int
58+
{
59+
$timezone = empty($timezone) ? app_timezone() : $timezone;
60+
61+
if ($timezone === 'local' || $timezone === date_default_timezone_get())
62+
{
63+
return time();
64+
}
65+
66+
$datetime = new DateTime('now', new DateTimeZone($timezone));
67+
sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second);
68+
69+
return mktime($hour, $minute, $second, $month, $day, $year);
70+
}
71+
}<?php
72+
/**
73+
* CodeIgniter
74+
*
75+
* An open source application development framework for PHP
76+
*
77+
* This content is released under the MIT License (MIT)
78+
*
79+
* Copyright (c) 2014-2019 British Columbia Institute of Technology
80+
*
81+
* Permission is hereby granted, free of charge, to any person obtaining a copy
82+
* of this software and associated documentation files (the "Software"), to deal
83+
* in the Software without restriction, including without limitation the rights
84+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
85+
* copies of the Software, and to permit persons to whom the Software is
86+
* furnished to do so, subject to the following conditions:
87+
*
88+
* The above copyright notice and this permission notice shall be included in
89+
* all copies or substantial portions of the Software.
90+
*
91+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
92+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
93+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
94+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
95+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
96+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
97+
* THE SOFTWARE.
98+
*
99+
* @package CodeIgniter
100+
* @author CodeIgniter Dev Team
101+
* @copyright 2014-2019 British Columbia Institute of Technology (https://bcit.ca/)
102+
* @license https://opensource.org/licenses/MIT MIT License
103+
* @link https://codeigniter.com
104+
* @since Version 4.0.0
105+
* @filesource
106+
*/
107+
108+
/**
109+
* CodeIgniter Date Helpers
110+
*
111+
* @package CodeIgniter
112+
*/
113+
44114
if (! function_exists('now'))
45115
{
46116
/**
@@ -86,17 +156,46 @@ function now(string $timezone = null): int
86156
* @throws \Exception
87157
*/
88158
function timezone_select(string $class = '', string $default = '', int $what = \DateTimeZone::ALL, string $country = null): string
89-
{
90-
$timezones = \DateTimeZone::listIdentifiers($what, $country);
91-
159+
{
160+
$zones = \DateTimeZone::listIdentifiers($what, $country);
161+
162+
// Get "common" prefix for each
163+
$tmpzones = [];
164+
foreach ($zones as $zone)
165+
{
166+
$date = new \DateTime('now', new \DateTimeZone($zone));
167+
$short = $date->format('T');
168+
169+
if (! isset($tmpzones[$short]))
170+
{
171+
$tmpzones[$short] = [];
172+
}
173+
$tmpzones[$short][] = $zone;
174+
}
175+
176+
// Sort the keys
177+
ksort($tmpzones);
178+
179+
// Create the final list which includes the true name as the key
180+
// and a display name that incorporates the short name in it
181+
$timezones = [];
182+
foreach ($tmpzones as $short => $list)
183+
{
184+
foreach ($list as $zone)
185+
{
186+
$timezones[$zone] = "{$short} - {$zone}";
187+
}
188+
}
189+
92190
$buffer = "<select name='timezone' class='{$class}'>" . PHP_EOL;
93-
foreach ($timezones as $timezone)
191+
foreach ($timezones as $zone => $display)
94192
{
95-
$selected = ($timezone == $default) ? 'selected' : '';
96-
$buffer .= "<option value='{$timezone}' {$selected}>{$timezone}</option>" . PHP_EOL;
193+
$selected = ($zone == $default) ? 'selected' : '';
194+
$buffer .= "<option value='{$zone}' {$selected}>{$display}</option>" . PHP_EOL;
97195
}
98196
$buffer .= "</select>" . PHP_EOL;
99-
197+
100198
return $buffer;
101-
}
199+
}
102200
}
201+

0 commit comments

Comments
 (0)