PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.2.2
LatePoint – Calendar Booking Plugin for Appointments and Events v5.2.2
5.6.7 5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / helpers / time_helper.php
latepoint / lib / helpers Last commit date
activities_helper.php 1 year ago agent_helper.php 1 year ago auth_helper.php 9 months ago blocks_helper.php 1 year ago booking_helper.php 1 year ago bricks_helper.php 1 year ago bundles_helper.php 1 year ago calendar_helper.php 9 months ago carts_helper.php 1 year ago connector_helper.php 1 year ago csv_helper.php 9 months ago customer_helper.php 9 months ago customer_import_helper.php 9 months ago database_helper.php 9 months ago debug_helper.php 1 year ago defaults_helper.php 1 year ago elementor_helper.php 1 year ago email_helper.php 9 months ago encrypt_helper.php 1 year ago events_helper.php 1 year ago form_helper.php 9 months ago icalendar_helper.php 1 year ago image_helper.php 1 year ago invoices_helper.php 9 months ago license_helper.php 1 year ago location_helper.php 1 year ago marketing_systems_helper.php 1 year ago meeting_systems_helper.php 1 year ago menu_helper.php 9 months ago meta_helper.php 1 year ago migrations_helper.php 1 year ago money_helper.php 1 year ago notifications_helper.php 9 months ago order_intent_helper.php 9 months ago orders_helper.php 1 year ago otp_helper.php 9 months ago pages_helper.php 1 year ago params_helper.php 1 year ago payments_helper.php 1 year ago price_breakdown_helper.php 1 year ago process_jobs_helper.php 1 year ago processes_helper.php 1 year ago replacer_helper.php 1 year ago resource_helper.php 1 year ago roles_helper.php 9 months ago router_helper.php 1 year ago service_helper.php 1 year ago sessions_helper.php 1 year ago settings_helper.php 9 months ago short_links_systems_helper.php 9 months ago shortcodes_helper.php 9 months ago sms_helper.php 1 year ago steps_helper.php 9 months ago stripe_connect_helper.php 9 months ago styles_helper.php 9 months ago support_topics_helper.php 1 year ago time_helper.php 9 months ago timeline_helper.php 9 months ago transaction_helper.php 1 year ago transaction_intent_helper.php 1 year ago util_helper.php 9 months ago version_specific_updates_helper.php 9 months ago whatsapp_helper.php 1 year ago work_periods_helper.php 1 year ago wp_datetime.php 1 year ago wp_user_helper.php 1 year ago
time_helper.php
563 lines
1 <?php
2
3 class OsTimeHelper {
4
5 private static $timezone = false;
6
7
8 public static function get_db_weekday_by_number( $number ) {
9 $weekdays = [ 'mo', 'tu', 'we', 'th', 'fr', 'sa', 'su' ];
10
11 return $weekdays[ $number - 1 ];
12 }
13
14 public static function is_valid_date( $date_string ) {
15 if(empty($date_string)) return false;
16 return (bool) strtotime( $date_string );
17 }
18
19 public static function reformat_date_string( $date_string, $from_format, $to_format ) {
20 $start_date_obj = OsWpDateTime::os_createFromFormat( $from_format, $date_string );
21
22 return $start_date_obj->format( $to_format );
23 }
24
25 public static function nice_date( $date ) {
26 if ( $date == OsTimeHelper::today_date( 'Y-m-d' ) ) {
27 $nice_date = __( 'Today', 'latepoint' );
28 } else {
29 $nice_date = self::get_nice_date_with_optional_year( $date, true );
30 }
31
32 return $nice_date;
33 }
34
35 public static function date_from_db( $date_string, $format = false, string $output_timezone_name = 'UTC' ) {
36 $timezone = new DateTimeZone( 'UTC' );
37 $date_obj = OsWpDateTime::os_createFromFormat( LATEPOINT_DATETIME_DB_FORMAT, $date_string, $timezone );
38 if(empty($date_obj)) $date_obj = self::now_datetime_object();
39 $date_obj->setTimezone( new DateTimeZone( $output_timezone_name ) );
40 if ( $format ) {
41 return $date_obj->format( $format );
42 }
43
44 return $date_obj;
45 }
46
47 public static function today_date( $date_format = 'Y-m-d' ) {
48 $today = new OsWpDateTime( 'today' );
49
50 return $today->format( $date_format );
51 }
52
53 public static function now_datetime_in_format( $date_format = LATEPOINT_DATETIME_DB_FORMAT, string $timezone = 'UTC' ) {
54 $now = self::now_datetime_object();
55 $now->setTimezone( new DateTimeZone( "UTC" ) );
56
57 return $now->format( $date_format );
58 }
59
60 public static function now_datetime_utc() {
61 $now = self::now_datetime_object();
62 $now->setTimezone( new DateTimeZone( "UTC" ) );
63
64 return $now;
65 }
66
67 public static function time_left_to_datetime( string $datetime, DateTimeZone $timezone ) {
68
69 $now_datetime = new OsWpDateTime( 'now', $timezone );
70 $event_datetime = OsWpDateTime::createFromFormat( LATEPOINT_DATETIME_DB_FORMAT, $datetime, $timezone );
71 $css_class = 'left-days';
72
73 $before = ( $now_datetime < $event_datetime ) ? __( 'in', 'latepoint' ) . ' ' : '';
74 $ago = ( $now_datetime > $event_datetime ) ? ' ' . __( 'ago', 'latepoint' ) : '';
75 $ago_class = empty( $ago ) ? '' : 'time-past';
76 if ( $event_datetime ) {
77 $diff = $now_datetime->diff( $event_datetime );
78 if ( $diff->d > 0 ) {
79 $left = $before . $diff->format( '%a ' . __( 'days', 'latepoint' ) ) . $ago;
80 } else {
81 if ( $diff->h > 0 ) {
82 $css_class = 'left-hours';
83 $left = $before . $diff->format( '%h ' . __( 'hours', 'latepoint' ) ) . $ago;
84 } else {
85 $css_class = 'left-minutes';
86 $left = $before . $diff->format( '%i ' . __( 'minutes', 'latepoint' ) ) . $ago;
87 }
88 }
89 } else {
90 $left = 'n/a';
91 }
92
93 return '<span class="time-left ' . esc_attr( $css_class ) . ' ' . esc_attr( $ago_class ) . '">' . esc_html( $left ) . '</span>';
94 }
95
96 public static function now_datetime_utc_in_db_format() {
97 return OsWpDateTime::datetime_in_utc( new OsWpDateTime( 'now' ), LATEPOINT_DATETIME_DB_FORMAT );
98 }
99
100 public static function custom_datetime_utc_in_db_format( string $time = 'now' ): string {
101 return OsWpDateTime::datetime_in_utc( new OsWpDateTime( $time ), LATEPOINT_DATETIME_DB_FORMAT );
102 }
103
104 public static function now_datetime_in_db_format() {
105 return self::now_datetime_in_format( LATEPOINT_DATETIME_DB_FORMAT );
106 }
107
108 public static function now_datetime_object() {
109 return new OsWpDateTime( 'now' );
110 }
111
112 public static function get_time_system() {
113 return OsSettingsHelper::get_time_system();
114 }
115
116 public static function get_time_format() {
117 return self::is_army_clock() ? 'H:i' : 'g:ia';
118 }
119
120 public static function is_army_clock() {
121 return ( self::get_time_system() == 24 );
122 }
123
124 public static function get_time_systems_list_for_select() {
125 return array(
126 array( 'value' => '12', 'label' => __( '12-hour clock', 'latepoint' ) ),
127 array( 'value' => '24', 'label' => __( '24-hour clock', 'latepoint' ) )
128 );
129 }
130
131 public static function get_date_formats_list_for_select() {
132 return array(
133 array( 'value' => 'm/d/Y', 'label' => __( 'MM/DD/YYYY', 'latepoint' ) ),
134 array( 'value' => 'm.d.Y', 'label' => __( 'MM.DD.YYYY', 'latepoint' ) ),
135 array( 'value' => 'd/m/Y', 'label' => __( 'DD/MM/YYYY', 'latepoint' ) ),
136 array( 'value' => 'd.m.Y', 'label' => __( 'DD.MM.YYYY', 'latepoint' ) ),
137 array( 'value' => 'Y-m-d', 'label' => __( 'YYYY-MM-DD', 'latepoint' ) )
138 );
139 }
140
141 public static function get_time_systems_list() {
142 return array( '12' => __( '12-hour clock', 'latepoint' ), '24' => __( '24-hour clock', 'latepoint' ) );
143 }
144
145
146 public static function format_date_with_locale( string $format, DateTime $date_obj ): string {
147 return OsUtilHelper::translate_months( $date_obj->format( $format ) );
148 }
149
150
151 public static function get_nice_date_with_optional_year( $date, $show_year_if_not_current = true, $short_month = false ) {
152 $d = OsWpDateTime::os_createFromFormat( "Y-m-d", $date );
153 if ( ! $d ) {
154 return $date;
155 }
156 if ( ! $show_year_if_not_current || ( $d->format( 'Y' ) == OsTimeHelper::today_date( 'Y' ) ) ) {
157 return OsUtilHelper::translate_months( $d->format( OsSettingsHelper::get_readable_date_format( true, $short_month ) ) );
158 } else {
159 return OsUtilHelper::translate_months( $d->format( OsSettingsHelper::get_readable_date_format( false, $short_month) ) );
160 }
161 }
162
163 public static function get_readable_date( DateTime $date, string $output_timezone_name = '' ): string {
164 if ( empty( $output_timezone_name ) ) {
165 $output_timezone_name = self::get_wp_timezone_name();
166 }
167 $date->setTimezone( new DateTimeZone( $output_timezone_name ) );
168
169 return self::format_date_with_locale( OsSettingsHelper::get_readable_date_format(), $date );
170 }
171
172 public static function get_readable_date_from_string( string $date, string $input_timezone_name = '', string $output_timezone_name = '' ): string {
173 if ( empty( $input_timezone_name ) ) {
174 $input_timezone_name = 'UTC';
175 }
176 if ( empty( $output_timezone_name ) ) {
177 $output_timezone_name = self::get_wp_timezone_name();
178 }
179
180 $date_obj = new OsWpDateTime( $date, new DateTimeZone( $input_timezone_name ) );
181
182 return self::get_readable_date( $date_obj, $output_timezone_name );
183 }
184
185 public static function get_wp_timezone() {
186 if ( self::$timezone ) {
187 return self::$timezone;
188 }
189 try{
190 $timezone_string = get_option( 'timezone_string' );
191 if ( ! empty( $timezone_string ) ) {
192 return new DateTimeZone( $timezone_string );
193 }
194 $offset = get_option( 'gmt_offset' );
195 $hours = (int) $offset;
196 $minutes = abs( ( $offset - (int) $offset ) * 60 );
197 $offset = sprintf( '%+03d:%02d', $hours, $minutes );
198 self::$timezone = new DateTimeZone( $offset );
199 }catch(Exception $e){
200 return new DateTimeZone('UTC');
201 }
202
203 return self::$timezone;
204 }
205
206 public static function get_wp_timezone_name() {
207 $timezone_obj = self::get_wp_timezone();
208 if ( $timezone_obj ) {
209 return $timezone_obj->getName();
210 } else {
211 return 'America/New_York';
212 }
213 }
214
215 public static function get_timezone_from_session() {
216 try {
217 $timezone = new DateTimeZone( self::get_timezone_name_from_session() );
218 } catch ( Exception $e ) {
219 $timezone = new DateTimeZone( self::get_wp_timezone_name() );
220 }
221
222 return $timezone;
223 }
224
225 public static function get_timezone_name_from_session() {
226 $timezone_name = self::get_wp_timezone_name();
227 $timezone_name = apply_filters( 'latepoint_timezone_name_from_session', $timezone_name );
228
229 return $timezone_name;
230 }
231
232 public static function is_timezone_saved_in_session() {
233 return ( isset( $_COOKIE[ LATEPOINT_SELECTED_TIMEZONE_COOKIE ] ) && ! empty( $_COOKIE[ LATEPOINT_SELECTED_TIMEZONE_COOKIE ] ) );
234 }
235
236 public static function set_timezone_name_in_cookie( $timezone_name ) {
237 OsSessionsHelper::setcookie( LATEPOINT_SELECTED_TIMEZONE_COOKIE, $timezone_name );
238 $_COOKIE[ LATEPOINT_SELECTED_TIMEZONE_COOKIE ] = $timezone_name;
239 }
240
241
242 public static function convert_datetime_to_minutes( DateTime $datetime ) {
243 return $datetime->format( 'i' ) + ( $datetime->format( 'G' ) * 60 );
244 }
245
246 public static function get_current_minutes( ) {
247 $now = new OsWpDateTime( 'now' );
248 return $now->format( 'i' ) + ( $now->format( 'G' ) * 60 );
249 }
250
251 public static function convert_time_to_minutes( $time, $ampm = false ) {
252 if ( strpos( $time, ':' ) === false ) {
253 return 0;
254 }
255
256 list( $hours, $minutes ) = explode( ':', $time );
257 if ( $hours == '12' && $ampm == 'am' ) {
258 // midnight
259 $hours = '0';
260 }
261 if ( $ampm == 'pm' && $hours < 12 ) {
262 // convert to 24 hour format
263 $hours = $hours + 12;
264 }
265 $minutes = ( $hours * 60 ) + $minutes;
266
267 return $minutes;
268 }
269
270 public static function am_or_pm( $minutes ) {
271 if ( self::is_army_clock() ) {
272 return '';
273 }
274
275 return ( $minutes < 720 ) ? 'am' : 'pm';
276 }
277
278 public static function minutes_to_hours( $time ) {
279 if ( $time ) {
280 $hours = floor( $time / 60 );
281 if ( ! self::is_army_clock() && $hours > 12 ) {
282 $hours = $hours - 12;
283 }
284 if ( ! self::is_army_clock() && ! $hours ) {
285 $hours = 12;
286 }
287
288 return $hours;
289 } else {
290 // if am/pm - we don't show 0, we show 12
291 return ( self::is_army_clock() ) ? 0 : 12;
292 }
293 }
294
295
296 public static function minutes_to_army_hours_and_minutes( $time_in_minutes ) {
297 if ( is_null( $time_in_minutes ) || $time_in_minutes === '' ) {
298 return __( 'n/a', 'latepoint' );
299 }
300 $hours = floor( $time_in_minutes / 60 );
301 $minutes = ( $time_in_minutes % 60 );
302
303 return sprintf( '%02d:%02d', $hours, $minutes );
304 }
305
306 public static function minutes_to_hours_and_minutes( $minutes, $format = '%02d:%02d', $add_ampm = true, $hide_if_zero_minutes = false ) {
307 if ( is_null( $minutes ) ) {
308 return 'n/a';
309 }
310 if ( ! $format ) {
311 $format = '%02d:%02d';
312 }
313
314 if ( $minutes === '' ) {
315 return;
316 }
317 $ampm = ( $add_ampm ) ? self::am_or_pm( $minutes ) : '';
318 $hours = self::minutes_to_hours( $minutes );
319 $minutes = ( $minutes % 60 );
320 if ( $hide_if_zero_minutes && ! $minutes ) {
321 return $hours . ' ' . $ampm;
322 } else {
323 return sprintf( $format, $hours, $minutes ) . $ampm;
324 }
325 }
326
327
328 public static function timezones_options_list_styled( $selected_zone, $locale = null ) {
329 static $mo_loaded = false, $locale_loaded = null;
330
331 $continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific' );
332
333 // Load translations for continents and cities.
334 if ( ! $mo_loaded || $locale !== $locale_loaded ) {
335 $locale_loaded = $locale ? $locale : get_locale();
336 $mofile = WP_LANG_DIR . '/continents-cities-' . $locale_loaded . '.mo';
337 unload_textdomain( 'continents-cities' );
338 load_textdomain( 'continents-cities', $mofile );
339 $mo_loaded = true;
340 }
341
342 $time_format = self::get_time_format();
343
344 $zonen = array();
345 foreach ( timezone_identifiers_list() as $zone ) {
346 $zone = explode( '/', $zone );
347 if ( ! in_array( $zone[0], $continents ) ) {
348 continue;
349 }
350
351 // This determines what gets set and translated - we don't translate Etc/* strings here, they are done later
352 $exists = array(
353 0 => ( isset( $zone[0] ) && $zone[0] ),
354 1 => ( isset( $zone[1] ) && $zone[1] ),
355 2 => ( isset( $zone[2] ) && $zone[2] ),
356 );
357 $exists[3] = ( $exists[0] && 'Etc' !== $zone[0] );
358 $exists[4] = ( $exists[1] && $exists[3] );
359 $exists[5] = ( $exists[2] && $exists[3] );
360
361 // phpcs:disable WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
362 $zonen[] = array(
363 'continent' => ( $exists[0] ? $zone[0] : '' ),
364 'city' => ( $exists[1] ? $zone[1] : '' ),
365 'subcity' => ( $exists[2] ? $zone[2] : '' ),
366 't_continent' => ( $exists[3] ? translate( str_replace( '_', ' ', $zone[0] ), 'continents-cities' ) : '' ),
367 't_city' => ( $exists[4] ? translate( str_replace( '_', ' ', $zone[1] ), 'continents-cities' ) : '' ),
368 't_subcity' => ( $exists[5] ? translate( str_replace( '_', ' ', $zone[2] ), 'continents-cities' ) : '' ),
369 );
370 // phpcs:enable
371 }
372 usort( $zonen, '_wp_timezone_choice_usort_callback' );
373
374 $structure = array();
375
376 foreach ( $zonen as $key => $zone ) {
377 // Build value in an array to join later
378 $value = array( $zone['continent'] );
379
380 if ( empty( $zone['city'] ) ) {
381 // It's at the continent level (generally won't happen)
382 $display = $zone['t_continent'];
383 } else {
384 // It's inside a continent group
385
386 // Continent optgroup
387 if ( ! isset( $zonen[ $key - 1 ] ) || $zonen[ $key - 1 ]['continent'] !== $zone['continent'] ) {
388 $label = $zone['t_continent'];
389 $structure[] = '<div class="os-timezone-group"><div class="os-timezone-group-header">'.esc_html( $label ).'</div>';
390 }
391
392 // Add the city to the value
393 $value[] = $zone['city'];
394
395 $display = $zone['t_city'];
396 if ( ! empty( $zone['subcity'] ) ) {
397 // Add the subcity to the value
398 $value[] = $zone['subcity'];
399 $display .= ' - ' . $zone['t_subcity'];
400 }
401 }
402
403 // Build the value
404 $value = join( '/', $value );
405 $selected = '';
406 if ( $value === $selected_zone ) {
407 $selected = 'selected';
408 }
409 try {
410 $local_time = new OsWpDateTime('now', new DateTimeZone($value));
411 $local_time_value = $local_time->format($time_format);
412 }catch(Exception $e){
413 $local_time_value = '';
414 }
415 $structure[] = '<div class="os-timezone-selector-option ' . $selected . '" data-value="' . esc_attr( $value ) . '"><div>' . esc_html( $display ) . '</div><div class="os-timezone-selector-option-local-time">'.$local_time_value.'</div></div>';
416
417 // Close continent optgroup
418 if ( ! empty( $zone['city'] ) && ( ! isset( $zonen[ $key + 1 ] ) || ( isset( $zonen[ $key + 1 ] ) && $zonen[ $key + 1 ]['continent'] !== $zone['continent'] ) ) ) {
419 $structure[] = '</div>';
420 }
421 }
422
423 // Do UTC
424 $structure[] = '<div class="os-timezone-group"><div class="os-timezone-group-header">'.esc_attr__( 'UTC', 'latepoint' ).'</div>';
425 $selected = '';
426 if ( 'UTC' === $selected_zone ) {
427 $selected = 'selected';
428 }
429 $structure[] = '<div class="os-timezone-selector-option ' . $selected . ' data-value="' . esc_attr( 'UTC' ) . '">' . __( 'UTC', 'latepoint' ) . '</div>';
430 $structure[] = '</div>';
431
432 return join( "\n", $structure );
433 }
434
435 public static function timezones_options_list( $selected_zone, $locale = null ) {
436 static $mo_loaded = false, $locale_loaded = null;
437
438 $continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific' );
439
440 // Load translations for continents and cities.
441 if ( ! $mo_loaded || $locale !== $locale_loaded ) {
442 $locale_loaded = $locale ? $locale : get_locale();
443 $mofile = WP_LANG_DIR . '/continents-cities-' . $locale_loaded . '.mo';
444 unload_textdomain( 'continents-cities' );
445 load_textdomain( 'continents-cities', $mofile );
446 $mo_loaded = true;
447 }
448
449 $zonen = array();
450 foreach ( timezone_identifiers_list() as $zone ) {
451 $zone = explode( '/', $zone );
452 if ( ! in_array( $zone[0], $continents ) ) {
453 continue;
454 }
455
456 // This determines what gets set and translated - we don't translate Etc/* strings here, they are done later
457 $exists = array(
458 0 => ( isset( $zone[0] ) && $zone[0] ),
459 1 => ( isset( $zone[1] ) && $zone[1] ),
460 2 => ( isset( $zone[2] ) && $zone[2] ),
461 );
462 $exists[3] = ( $exists[0] && 'Etc' !== $zone[0] );
463 $exists[4] = ( $exists[1] && $exists[3] );
464 $exists[5] = ( $exists[2] && $exists[3] );
465
466 // phpcs:disable WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
467 $zonen[] = array(
468 'continent' => ( $exists[0] ? $zone[0] : '' ),
469 'city' => ( $exists[1] ? $zone[1] : '' ),
470 'subcity' => ( $exists[2] ? $zone[2] : '' ),
471 't_continent' => ( $exists[3] ? translate( str_replace( '_', ' ', $zone[0] ), 'continents-cities' ) : '' ),
472 't_city' => ( $exists[4] ? translate( str_replace( '_', ' ', $zone[1] ), 'continents-cities' ) : '' ),
473 't_subcity' => ( $exists[5] ? translate( str_replace( '_', ' ', $zone[2] ), 'continents-cities' ) : '' ),
474 );
475 // phpcs:enable
476 }
477 usort( $zonen, '_wp_timezone_choice_usort_callback' );
478
479 $structure = array();
480
481 if ( empty( $selected_zone ) ) {
482 $structure[] = '<option selected="selected" value="">' . __( 'Select a city', 'latepoint' ) . '</option>';
483 }
484
485 foreach ( $zonen as $key => $zone ) {
486 // Build value in an array to join later
487 $value = array( $zone['continent'] );
488
489 if ( empty( $zone['city'] ) ) {
490 // It's at the continent level (generally won't happen)
491 $display = $zone['t_continent'];
492 } else {
493 // It's inside a continent group
494
495 // Continent optgroup
496 if ( ! isset( $zonen[ $key - 1 ] ) || $zonen[ $key - 1 ]['continent'] !== $zone['continent'] ) {
497 $label = $zone['t_continent'];
498 $structure[] = '<optgroup label="' . esc_attr( $label ) . '">';
499 }
500
501 // Add the city to the value
502 $value[] = $zone['city'];
503
504 $display = $zone['t_city'];
505 if ( ! empty( $zone['subcity'] ) ) {
506 // Add the subcity to the value
507 $value[] = $zone['subcity'];
508 $display .= ' - ' . $zone['t_subcity'];
509 }
510 }
511
512 // Build the value
513 $value = join( '/', $value );
514 $selected = '';
515 if ( $value === $selected_zone ) {
516 $selected = 'selected="selected" ';
517 }
518 $structure[] = '<option ' . $selected . 'value="' . esc_attr( $value ) . '">' . esc_html( $label ) . ', ' . esc_html( $display ) . '</option>';
519
520 // Close continent optgroup
521 if ( ! empty( $zone['city'] ) && ( ! isset( $zonen[ $key + 1 ] ) || ( isset( $zonen[ $key + 1 ] ) && $zonen[ $key + 1 ]['continent'] !== $zone['continent'] ) ) ) {
522 $structure[] = '</optgroup>';
523 }
524 }
525
526 // Do UTC
527 $structure[] = '<optgroup label="' . esc_attr__( 'UTC', 'latepoint' ) . '">';
528 $selected = '';
529 if ( 'UTC' === $selected_zone ) {
530 $selected = 'selected="selected" ';
531 }
532 $structure[] = '<option ' . $selected . 'value="' . esc_attr( 'UTC' ) . '">' . __( 'UTC', 'latepoint' ) . '</option>';
533 $structure[] = '</optgroup>';
534
535 return join( "\n", $structure );
536 }
537
538 public static function format_to_nice_time( $datetime ): string {
539 if ( $datetime instanceof DateTime ) {
540 $format = self::is_army_clock() ? 'H:i' : 'g:ia';
541
542 return $datetime->format( $format );
543 } else {
544 return 'n/a';
545 }
546 }
547
548 public static function format_to_nice_date( $datetime, $hide_year_if_current = false ): string {
549 if ( $datetime instanceof DateTime ) {
550 if ( $hide_year_if_current && ( $datetime->format( 'Y' ) == self::today_date( 'Y' ) ) ) {
551 $format = OsSettingsHelper::get_readable_date_format( true );
552 } else {
553 $format = OsSettingsHelper::get_readable_date_format();
554 }
555
556 return OsUtilHelper::translate_months( $datetime->format( $format ) );
557 } else {
558 return 'n/a';
559 }
560
561 }
562
563 }