PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.1.7
LatePoint – Calendar Booking Plugin for Appointments and Events v5.1.7
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 1 year 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 1 year ago carts_helper.php 1 year ago connector_helper.php 1 year ago csv_helper.php 1 year ago customer_helper.php 1 year ago database_helper.php 1 year ago debug_helper.php 1 year ago defaults_helper.php 1 year ago elementor_helper.php 1 year ago email_helper.php 1 year ago encrypt_helper.php 1 year ago events_helper.php 1 year ago form_helper.php 1 year ago icalendar_helper.php 1 year ago image_helper.php 1 year ago invoices_helper.php 1 year 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 1 year ago meta_helper.php 1 year ago migrations_helper.php 1 year ago money_helper.php 1 year ago notifications_helper.php 1 year ago order_intent_helper.php 1 year ago orders_helper.php 1 year 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 1 year ago router_helper.php 1 year ago service_helper.php 1 year ago sessions_helper.php 1 year ago settings_helper.php 1 year ago shortcodes_helper.php 1 year ago sms_helper.php 1 year ago steps_helper.php 1 year ago stripe_connect_helper.php 1 year ago styles_helper.php 1 year ago support_topics_helper.php 1 year ago time_helper.php 1 year ago timeline_helper.php 1 year ago transaction_intent_helper.php 1 year ago util_helper.php 1 year ago version_specific_updates_helper.php 1 year 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
450 lines
1 <?php
2 class OsTimeHelper {
3
4 private static $timezone = false;
5
6
7 public static function get_db_weekday_by_number($number){
8 $weekdays = ['mo','tu','we','th','fr','sa','su'];
9 return $weekdays[$number - 1];
10 }
11
12 public static function is_valid_date($date_string){
13 return (bool)strtotime($date_string);
14 }
15
16 public static function reformat_date_string($date_string, $from_format, $to_format){
17 $start_date_obj = OsWpDateTime::os_createFromFormat($from_format, $date_string);
18 return $start_date_obj->format($to_format);
19 }
20
21 public static function shift_time_by_minutes($time_in_minutes, $shift_in_minutes = 0){
22 if(is_null($time_in_minutes)) return $time_in_minutes;
23 if($shift_in_minutes){
24 $time_in_minutes = $time_in_minutes + $shift_in_minutes;
25 if($time_in_minutes > (24 * 60)){
26 $time_in_minutes = $time_in_minutes - (24 * 60);
27 }elseif($time_in_minutes < 0){
28 $time_in_minutes = $time_in_minutes + (24 * 60);
29 }
30 }
31 return $time_in_minutes;
32 }
33
34
35 public static function shift_date_by_minutes($date, $time_in_minutes, $shift_in_minutes = 0){
36 if($shift_in_minutes){
37 $time_in_minutes = $time_in_minutes + $shift_in_minutes;
38 if($time_in_minutes > (24 * 60)){
39 $date = OsTimeHelper::modify_date($date, '+1 day');
40 }elseif($time_in_minutes < 0){
41 $date = OsTimeHelper::modify_date($date, '-1 day');
42 }
43 }
44 return $date;
45 }
46
47 public static function modify_date($date, $modify_by = '+1 day', $format = 'Y-m-d'){
48 $date_obj = new DateTime( $date );
49 return $date_obj->modify($modify_by)->format($format);
50 }
51
52 public static function nice_date($date){
53 if($date == OsTimeHelper::today_date('Y-m-d')){
54 $nice_date = __('Today', 'latepoint');
55 }else{
56 $nice_date = self::get_nice_date_with_optional_year($date, true);
57 }
58 return $nice_date;
59 }
60
61 public static function date_from_db($date_string, $format = false, string $output_timezone_name = 'UTC'){
62 $timezone = new DateTimeZone('UTC');
63 $date_obj = OsWpDateTime::os_createFromFormat(LATEPOINT_DATETIME_DB_FORMAT, $date_string, $timezone);
64 $date_obj->setTimezone(new DateTimeZone($output_timezone_name));
65 if($format) return $date_obj->format($format);
66 return $date_obj;
67 }
68
69 public static function today_date($date_format = 'Y-m-d'){
70 $today = new OsWpDateTime('today');
71 return $today->format($date_format);
72 }
73
74 public static function now_datetime_in_format($date_format = LATEPOINT_DATETIME_DB_FORMAT, string $timezone = 'UTC'){
75 $now = self::now_datetime_object();
76 $now->setTimezone(new DateTimeZone("UTC"));
77 return $now->format($date_format);
78 }
79
80 public static function now_datetime_utc(){
81 $now = self::now_datetime_object();
82 $now->setTimezone(new DateTimeZone("UTC"));
83 return $now;
84 }
85
86 public static function time_left_to_datetime(string $datetime, DateTimeZone $timezone){
87
88 $now_datetime = new OsWpDateTime('now', $timezone);
89 $event_datetime = OsWpDateTime::createFromFormat(LATEPOINT_DATETIME_DB_FORMAT, $datetime, $timezone);
90 $css_class = 'left-days';
91
92 $before = ($now_datetime < $event_datetime) ? __('in', 'latepoint').' ' : '';
93 $ago = ($now_datetime > $event_datetime) ? ' '.__('ago', 'latepoint') : '';
94 $ago_class = empty($ago) ? '' : 'time-past';
95 if($event_datetime){
96 $diff = $now_datetime->diff($event_datetime);
97 if($diff->d > 0){
98 $left = $before.$diff->format('%a '.__('days', 'latepoint')).$ago;
99 }else{
100 if($diff->h > 0){
101 $css_class = 'left-hours';
102 $left = $before.$diff->format('%h '.__('hours', 'latepoint')).$ago;
103 }else{
104 $css_class = 'left-minutes';
105 $left = $before.$diff->format('%i '.__('minutes', 'latepoint')).$ago;
106 }
107 }
108 }else{
109 $left = 'n/a';
110 }
111
112 return '<span class="time-left '.esc_attr($css_class).' '.esc_attr($ago_class).'">'.esc_html($left).'</span>';
113 }
114
115 public static function now_datetime_utc_in_db_format(){
116 return OsWpDateTime::datetime_in_utc(new OsWpDateTime('now'), LATEPOINT_DATETIME_DB_FORMAT);
117 }
118
119 public static function custom_datetime_utc_in_db_format(string $time = 'now') : string{
120 return OsWpDateTime::datetime_in_utc(new OsWpDateTime($time), LATEPOINT_DATETIME_DB_FORMAT);
121 }
122
123 public static function now_datetime_in_db_format(){
124 return self::now_datetime_in_format(LATEPOINT_DATETIME_DB_FORMAT);
125 }
126
127 public static function get_modified_now_object($modify_by){
128 $now_datetime = self::now_datetime_object();
129 return $now_datetime->modify($modify_by);
130 }
131
132 public static function now_datetime_object(){
133 return new OsWpDateTime('now');
134 }
135
136 public static function get_time_system(){
137 return OsSettingsHelper::get_time_system();
138 }
139
140 public static function get_time_format(){
141 return self::is_army_clock() ? 'H:i' : 'g:ia';
142 }
143
144 public static function is_army_clock(){
145 return (self::get_time_system() == 24);
146 }
147
148 public static function get_time_systems_list_for_select(){
149 return array( array( 'value' => '12', 'label' => __('12-hour clock', 'latepoint')),
150 array( 'value' => '24', 'label' => __('24-hour clock', 'latepoint')));
151 }
152
153 public static function get_date_formats_list_for_select(){
154 return array( array( 'value' => 'm/d/Y', 'label' => __('MM/DD/YYYY', 'latepoint')),
155 array( 'value' => 'm.d.Y', 'label' => __('MM.DD.YYYY', 'latepoint')),
156 array( 'value' => 'd/m/Y', 'label' => __('DD/MM/YYYY', 'latepoint')),
157 array( 'value' => 'd.m.Y', 'label' => __('DD.MM.YYYY', 'latepoint')),
158 array( 'value' => 'Y-m-d', 'label' => __('YYYY-MM-DD', 'latepoint')));
159 }
160
161 public static function get_time_systems_list(){
162 return array('12' => __('12-hour clock', 'latepoint'), '24' => __('24-hour clock', 'latepoint'));
163 }
164
165
166 public static function format_date_with_locale(string $format, DateTime $date_obj): string{
167 return OsUtilHelper::translate_months($date_obj->format($format));
168 }
169
170
171 public static function get_nice_date_with_optional_year($date, $show_year_if_not_current = true){
172 $d = OsWpDateTime::os_createFromFormat("Y-m-d", $date);
173 if(!$d) return $date;
174 if(!$show_year_if_not_current || ($d->format('Y') == OsTimeHelper::today_date('Y'))){
175 return OsUtilHelper::translate_months($d->format(OsSettingsHelper::get_readable_date_format(true)));
176 }else{
177 return OsUtilHelper::translate_months($d->format(OsSettingsHelper::get_readable_date_format()));
178 }
179 }
180
181 public static function get_readable_date(DateTime $date, string $output_timezone_name = ''): string{
182 if(empty($output_timezone_name)) $output_timezone_name = self::get_wp_timezone_name();
183 $date->setTimezone(new DateTimeZone($output_timezone_name));
184 return self::format_date_with_locale(OsSettingsHelper::get_readable_date_format(), $date);
185 }
186
187 public static function get_readable_date_from_string(string $date, string $input_timezone_name = '', string $output_timezone_name = ''): string{
188 if(empty($input_timezone_name)) $input_timezone_name = 'UTC';
189 if(empty($output_timezone_name)) $output_timezone_name = self::get_wp_timezone_name();
190
191 $date_obj = new OsWpDateTime($date, new DateTimeZone($input_timezone_name));
192 return self::get_readable_date($date_obj, $output_timezone_name);
193 }
194
195 public static function get_wp_timezone() {
196 if(self::$timezone) return self::$timezone;
197 $timezone_string = get_option( 'timezone_string' );
198 if ( ! empty( $timezone_string ) ) {
199 return new DateTimeZone( $timezone_string );
200 }
201 $offset = get_option( 'gmt_offset' );
202 $hours = (int) $offset;
203 $minutes = abs( ( $offset - (int) $offset ) * 60 );
204 $offset = sprintf( '%+03d:%02d', $hours, $minutes );
205 self::$timezone = new DateTimeZone( $offset );
206 return self::$timezone;
207 }
208
209 public static function get_wp_timezone_name() {
210 $timezone_obj = self::get_wp_timezone();
211 if($timezone_obj){
212 return $timezone_obj->getName();
213 }else{
214 return 'America/New_York';
215 }
216 }
217
218 public static function get_timezone_from_session(){
219 $timezone = new DateTimeZone(self::get_timezone_name_from_session());
220 return $timezone;
221 }
222
223 public static function get_timezone_name_from_session(){
224 $timezone_name = self::get_wp_timezone_name();
225 $timezone_name = apply_filters('latepoint_timezone_name_from_session', $timezone_name);
226 return $timezone_name;
227 }
228
229 public static function is_timezone_saved_in_session(){
230 return (isset($_COOKIE[LATEPOINT_SELECTED_TIMEZONE_COOKIE]) && !empty($_COOKIE[LATEPOINT_SELECTED_TIMEZONE_COOKIE]));
231 }
232
233 public static function set_timezone_name_in_cookie($timezone_name){
234 OsSessionsHelper::setcookie(LATEPOINT_SELECTED_TIMEZONE_COOKIE, $timezone_name);
235 $_COOKIE[LATEPOINT_SELECTED_TIMEZONE_COOKIE] = $timezone_name;
236 }
237
238 public static function get_timezone_shift_in_minutes_from_session(){
239 return self::get_timezone_shift_in_minutes(self::get_timezone_name_from_session());
240 }
241
242
243 public static function get_timezone_shift_in_minutes($requested_timezone_name){
244 if(self::get_wp_timezone_name() == $requested_timezone_name) return 0;
245 $wp_timezone = self::get_wp_timezone();
246 try{
247 $requested_timezone = new DateTimeZone( $requested_timezone_name );
248 }catch(Exception $e){
249 $requested_timezone = self::get_wp_timezone();
250 }
251
252 $now_in_wp_tz = new DateTime('now', $wp_timezone);
253 $now_in_requested_tz = new DateTime('now', $requested_timezone);
254
255 $offset = $requested_timezone->getOffset($now_in_requested_tz) - $wp_timezone->getOffset($now_in_wp_tz);
256 $shift_in_minutes = round($offset / 60);
257 return $shift_in_minutes;
258 }
259
260 public static function get_timezone_shift_from_gmt_in_minutes($requested_timezone_name){
261 $utc_timezone = new DateTimeZone( 'UTC' );
262 try{
263 $requested_timezone = new DateTimeZone( $requested_timezone_name );
264 }catch(Exception $e){
265 $requested_timezone = self::get_wp_timezone();
266 }
267
268 $now_in_utc_tz = new DateTime('now', $utc_timezone);
269 $now_in_requested_tz = new DateTime('now', $requested_timezone);
270
271 $offset = $requested_timezone->getOffset($now_in_requested_tz) - $utc_timezone->getOffset($now_in_utc_tz);
272 $shift_in_minutes = round($offset / 60);
273 return $shift_in_minutes;
274 }
275
276
277 public static function convert_datetime_to_minutes(DateTime $datetime){
278 return $datetime->format('i') + ($datetime->format('G') * 60);
279 }
280
281 public static function get_current_minutes($timeshift_minutes = false){
282 $now = new OsWpDateTime('now');
283 if($timeshift_minutes) $now->modify($timeshift_minutes.' minutes');
284 return $now->format('i') + ($now->format('G') * 60);
285 }
286
287 public static function convert_time_to_minutes($time, $ampm = false){
288 if(strpos($time, ':') === false) return 0;
289
290 list($hours, $minutes) = explode(':', $time);
291 if($hours == '12' && $ampm == 'am'){
292 // midnight
293 $hours = '0';
294 }
295 if($ampm == 'pm' && $hours < 12){
296 // convert to 24 hour format
297 $hours = $hours + 12;
298 }
299 $minutes = ($hours * 60) + $minutes;
300 return $minutes;
301 }
302
303 public static function am_or_pm($minutes) {
304 if(self::is_army_clock()) return '';
305 return ($minutes < 720) ? 'am' : 'pm';
306 }
307
308 public static function minutes_to_hours($time) {
309 if($time){
310 $hours = floor($time / 60);
311 if(!self::is_army_clock() && $hours > 12) $hours = $hours - 12;
312 if(!self::is_army_clock() && !$hours) $hours = 12;
313 return $hours;
314 }else{
315 // if am/pm - we don't show 0, we show 12
316 return (self::is_army_clock()) ? 0 : 12;
317 }
318 }
319
320
321 public static function minutes_to_army_hours_and_minutes($time_in_minutes) {
322 if(is_null($time_in_minutes) || $time_in_minutes === '') return __('n/a', 'latepoint');
323 $hours = floor($time_in_minutes / 60);
324 $minutes = ($time_in_minutes % 60);
325 return sprintf('%02d:%02d', $hours, $minutes);
326 }
327
328 public static function minutes_to_hours_and_minutes($minutes, $format = '%02d:%02d', $add_ampm = true, $hide_if_zero_minutes = false)
329 {
330 if(is_null($minutes)) return 'n/a';
331 if (!$format) $format = '%02d:%02d';
332
333 if ($minutes === '') {
334 return;
335 }
336 $ampm = ($add_ampm) ? self::am_or_pm($minutes) : '';
337 $hours = self::minutes_to_hours($minutes);
338 $minutes = ($minutes % 60);
339 if($hide_if_zero_minutes && !$minutes){
340 return $hours . ' ' . $ampm;
341 }else{
342 return sprintf($format, $hours, $minutes).$ampm;
343 }
344 }
345
346
347 public static function timezones_options_list( $selected_zone, $locale = null ) {
348 static $mo_loaded = false, $locale_loaded = null;
349
350 $continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific' );
351
352 // Load translations for continents and cities.
353 if ( ! $mo_loaded || $locale !== $locale_loaded ) {
354 $locale_loaded = $locale ? $locale : get_locale();
355 $mofile = WP_LANG_DIR . '/continents-cities-' . $locale_loaded . '.mo';
356 unload_textdomain( 'continents-cities' );
357 load_textdomain( 'continents-cities', $mofile );
358 $mo_loaded = true;
359 }
360
361 $zonen = array();
362 foreach ( timezone_identifiers_list() as $zone ) {
363 $zone = explode( '/', $zone );
364 if ( ! in_array( $zone[0], $continents ) ) {
365 continue;
366 }
367
368 // This determines what gets set and translated - we don't translate Etc/* strings here, they are done later
369 $exists = array(
370 0 => ( isset( $zone[0] ) && $zone[0] ),
371 1 => ( isset( $zone[1] ) && $zone[1] ),
372 2 => ( isset( $zone[2] ) && $zone[2] ),
373 );
374 $exists[3] = ( $exists[0] && 'Etc' !== $zone[0] );
375 $exists[4] = ( $exists[1] && $exists[3] );
376 $exists[5] = ( $exists[2] && $exists[3] );
377
378 // phpcs:disable WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
379 $zonen[] = array(
380 'continent' => ( $exists[0] ? $zone[0] : '' ),
381 'city' => ( $exists[1] ? $zone[1] : '' ),
382 'subcity' => ( $exists[2] ? $zone[2] : '' ),
383 't_continent' => ( $exists[3] ? translate( str_replace( '_', ' ', $zone[0] ), 'continents-cities' ) : '' ),
384 't_city' => ( $exists[4] ? translate( str_replace( '_', ' ', $zone[1] ), 'continents-cities' ) : '' ),
385 't_subcity' => ( $exists[5] ? translate( str_replace( '_', ' ', $zone[2] ), 'continents-cities' ) : '' ),
386 );
387 // phpcs:enable
388 }
389 usort( $zonen, '_wp_timezone_choice_usort_callback' );
390
391 $structure = array();
392
393 if ( empty( $selected_zone ) ) {
394 $structure[] = '<option selected="selected" value="">' . __( 'Select a city', 'latepoint' ) . '</option>';
395 }
396
397 foreach ( $zonen as $key => $zone ) {
398 // Build value in an array to join later
399 $value = array( $zone['continent'] );
400
401 if ( empty( $zone['city'] ) ) {
402 // It's at the continent level (generally won't happen)
403 $display = $zone['t_continent'];
404 } else {
405 // It's inside a continent group
406
407 // Continent optgroup
408 if ( ! isset( $zonen[ $key - 1 ] ) || $zonen[ $key - 1 ]['continent'] !== $zone['continent'] ) {
409 $label = $zone['t_continent'];
410 $structure[] = '<optgroup label="' . esc_attr( $label ) . '">';
411 }
412
413 // Add the city to the value
414 $value[] = $zone['city'];
415
416 $display = $zone['t_city'];
417 if ( ! empty( $zone['subcity'] ) ) {
418 // Add the subcity to the value
419 $value[] = $zone['subcity'];
420 $display .= ' - ' . $zone['t_subcity'];
421 }
422 }
423
424 // Build the value
425 $value = join( '/', $value );
426 $selected = '';
427 if ( $value === $selected_zone ) {
428 $selected = 'selected="selected" ';
429 }
430 $structure[] = '<option ' . $selected . 'value="' . esc_attr( $value ) . '">' .esc_html( $label ).', '. esc_html( $display ) . '</option>';
431
432 // Close continent optgroup
433 if ( ! empty( $zone['city'] ) && ( ! isset( $zonen[ $key + 1 ] ) || ( isset( $zonen[ $key + 1 ] ) && $zonen[ $key + 1 ]['continent'] !== $zone['continent'] ) ) ) {
434 $structure[] = '</optgroup>';
435 }
436 }
437
438 // Do UTC
439 $structure[] = '<optgroup label="' . esc_attr__( 'UTC', 'latepoint' ) . '">';
440 $selected = '';
441 if ( 'UTC' === $selected_zone ) {
442 $selected = 'selected="selected" ';
443 }
444 $structure[] = '<option ' . $selected . 'value="' . esc_attr( 'UTC' ) . '">' . __( 'UTC', 'latepoint' ) . '</option>';
445 $structure[] = '</optgroup>';
446
447 return join( "\n", $structure );
448 }
449
450 }