PluginProbe
Booking Calendar / 10.1.3
Booking Calendar v10.1.3
11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 10.11 10.11.2 All 203 releases
booking / core / wpbc_functions_dates.php

wpbc_functions_dates.php in Booking Calendar 10.1.3, at core/wpbc_functions_dates.php

583 lines 19.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
5 // ---------------------------------------------------------------------------------------------------------------------
6 // Localize dates
7 // ---------------------------------------------------------------------------------------------------------------------
8
9
10 /**
11 * Return date / time in 'LOCAL_FORMAT'.
12 *
13 * @param string|int $date_str_ymdhis Date to format.
14 * @param string $format Optional. Date/Time Format, like 'Y-m-d H:i:s'
15 * @param bool $is_add_timezone_offset Optional. Timezone offset.
16 *
17 * @return string
18 */
19 function wpbc_datetime_localized( $date_str_ymdhis, $format = '', $is_add_timezone_offset = false ) {
20
21 if ( empty( $format ) ) {
22 $format = sprintf( '%s %s', get_option( 'booking_date_format' ), get_option( 'booking_time_format' ) );
23 }
24 if ( empty( $format ) ) {
25 $format = sprintf( '%s %s', get_option( 'date_format' ), get_option( 'time_format' ) );
26 }
27
28 $server_zone = date_default_timezone_get(); // If in 'Theme' or 'other plugin' set default timezone other than UTC. Save it.
29 if ( 'UTC' !== $server_zone ) { // Needed for WP date functions - set timezone to UTC
30 @date_default_timezone_set( 'UTC' );
31 }
32
33 if ( is_string( $date_str_ymdhis ) ) {
34 $date_str_ymdhis = strtotime( $date_str_ymdhis );
35 }
36
37 if ( $is_add_timezone_offset ) {
38 $date_str_ymdhis += (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
39 }
40
41 $local_date = date_i18n( $format, $date_str_ymdhis );
42
43 if ( 'UTC' !== $server_zone ) { // Back to previos state, if it was changed.
44 @date_default_timezone_set( $server_zone );
45 }
46
47 return $local_date;
48 }
49
50 // -----------------------------------------------------------------------------------------------------------------
51
52 //FixIn: 9.9.0.17
53 /**
54 * Return date / time in 'LOCAL_FORMAT' with WordPress Timezone offset from WordPress > Settings General page
55 *
56 * @param string|int $date_str_ymdhis Date to format.
57 * @param string $format Optional. Date/Time Format, like 'Y-m-d H:i:s'
58 *
59 * @return string
60 */
61 function wpbc_datetime_localized__use_wp_timezone( $date_str_ymdhis, $format = '') {
62
63 $is_add_timezone_offset = true;
64
65 $datetime_localized = wpbc_datetime_localized( $date_str_ymdhis, $format, $is_add_timezone_offset );
66
67 return $datetime_localized;
68 }
69
70 /**
71 * Return date / time in 'LOCAL_FORMAT' without WordPress Timezone offset - NO TIMEZONE
72 *
73 * @param string|int $date_str_ymdhis Date to format.
74 * @param string $format Optional. Date/Time Format, like 'Y-m-d H:i:s'
75 *
76 * @return string
77 */
78 function wpbc_datetime_localized__no_wp_timezone( $date_str_ymdhis, $format = '') {
79
80 $is_add_timezone_offset = false;
81
82 $datetime_localized = wpbc_datetime_localized( $date_str_ymdhis, $format, $is_add_timezone_offset );
83
84 return $datetime_localized;
85 }
86
87
88 // -----------------------------------------------------------------------------------------------------------------
89
90 /**
91 * Return date / time in 'LOCAL_FORMAT' with WordPress Timezone offset from WordPress > Settings General page
92 *
93 * @param string $format Optional. Date/Time Format, like 'Y-m-d H:i:s'
94 * @param string|int $date_str_ymdhis Date to format.
95 *
96 * @return string
97 */
98 function wpbc_datetime__use_wp_timezone( $format, $date_str_ymdhis = '' ) {
99
100 if ( '' === $date_str_ymdhis ) {
101 $date_str_ymdhis = date( 'Y-m-d H:i:s', strtotime( 'now' ) );
102 }
103
104 return wpbc_datetime_localized__use_wp_timezone( $date_str_ymdhis, $format );
105 }
106
107
108 /**
109 * Return date / time in 'LOCAL_FORMAT' without WordPress Timezone offset - NO TIMEZONE
110 *
111 * @param string $format Optional. Date/Time Format, like 'Y-m-d H:i:s'
112 * @param string|int $date_str_ymdhis Date to format.
113 *
114 * @return string
115 */
116 function wpbc_datetime__no_wp_timezone( $format, $date_str_ymdhis = '' ) {
117
118 if ( '' === $date_str_ymdhis ) {
119 $date_str_ymdhis = date( 'Y-m-d H:i:s', strtotime( 'now' ) );
120 }
121
122 return wpbc_datetime_localized__no_wp_timezone( $date_str_ymdhis, $format );
123 }
124
125
126 // -----------------------------------------------------------------------------------------------------------------
127
128
129 /**
130 * Return date in 'LOCAL_FORMAT'.
131 *
132 * @param string|int $date_str_ymd Date to format.
133 * @param string $format Optional. Date/Time Format, like 'Y-m-d'
134 * @param bool $is_add_timezone_offset Optional. Timezone offset.
135 *
136 * @return string
137 */
138 function wpbc_date_localized( $date_str_ymd, $format = '', $is_add_timezone_offset = false ) {
139
140 if ( empty( $format ) ) {
141 $format = get_option( 'booking_date_format' );
142 }
143 if ( $format === '' ) {
144 $format = get_option( 'date_format' );
145 }
146
147 return wpbc_datetime_localized( $date_str_ymd, $format, $is_add_timezone_offset );
148 }
149
150 /**
151 * Return date in 'LOCAL_FORMAT'.
152 *
153 * @param string|int $time_str_his Date to format.
154 * @param string $format Optional. Date/Time Format, like 'Y-m-d'
155 * @param bool $is_add_timezone_offset Optional. Timezone offset.
156 *
157 * @return string
158 */
159 function wpbc_time_localized( $time_str_his, $format = '', $is_add_timezone_offset = false ) {
160
161 if ( empty( $format ) ) {
162 $format = get_option( 'booking_time_format' );
163 }
164 if ( $format === '' ) {
165 $format = get_option( 'time_format' );
166 }
167
168 return wpbc_datetime_localized( $time_str_his, $format, $is_add_timezone_offset );
169 }
170
171
172 // ---------------------------------------------------------------------------------------------------------------------
173 // Localize support
174 // ---------------------------------------------------------------------------------------------------------------------
175
176 /**
177 * Get localized Dates as comma separated string from SQL comma separated Dates
178 *
179 * @param string $dates_str_in_sql_format - '2015-02-29 00:00:00, 2015-02-30 00:00:00' | '2015-02-29, 2015-02-30' | '2015-02-29 16:00:00, 2015-02-30 12:00:00'
180 *
181 * @return string - localized comma separated Dates string
182 */
183 function wpbc_get_dates_comma_string_localized( $dates_str_in_sql_format ) {
184
185 if ( empty( $dates_str_in_sql_format ) ) {
186 return '';
187 }
188
189 $dates_array_in_sql_format = explode( ',', $dates_str_in_sql_format );
190
191 $result_dates_arr = array();
192
193 foreach ( $dates_array_in_sql_format as $date_sql ) {
194
195 $date_sql = trim( $date_sql );
196
197 $date_arr = explode( ' ', $date_sql );
198
199 if ( count( $date_arr ) > 1 ) {
200 $time_arr = explode( ':', $date_arr[1] );
201 } else {
202 $time_arr = array( '00', '00', '00' );
203 }
204
205 if ( $time_arr == array( '00', '00', '00' ) ) {
206 $result_dates_arr[] = wpbc_date_localized( $date_sql ); // Only Date
207 } else {
208 $result_dates_arr[] = wpbc_datetime_localized( $date_sql ); // Date and Time
209 }
210 }
211
212 $mydates_result = implode( ', ', $result_dates_arr );
213
214 return $mydates_result;
215 }
216
217
218 // ---------------------------------------------------------------------------------------------------------------------
219 // Readable dates
220 // ---------------------------------------------------------------------------------------------------------------------
221
222 /**
223 * Get readable dates for showing text to Users.
224 *
225 * @param $dates_ymd_arr [ '2023-10-11', '2023-11-12', '2023-11-13' ]
226 * @param $params [ ] Optional
227 *
228 * @return string
229 */
230 function wpbc_get_redable_dates( $dates_ymd_arr , $params = array() ){
231
232 $defaults = array(
233 'is_use_booking_recurrent_time' => ( 'On' === get_bk_option( 'booking_recurrent_time' ) ),
234 'output_dates' => get_bk_option( 'booking_date_view_type' ), // 'short' | 'wide'
235 'date_label' => ( wpbc_is_multiple_dates( $dates_ymd_arr ) ) ? __( 'Dates', 'booking' ) : __( 'Date', 'booking' ),
236 'is_in_html' => true
237 );
238 $params = wp_parse_args( $params, $defaults );
239
240 if ( 'short' == $params['output_dates'] ) {
241 $dates_view = wpbc_get_dates_short_format( implode( ',', $dates_ymd_arr ) ); // Short dates: '01 Jan 2023 - 30 Jan 2023'
242 } else {
243 $dates_view = wpbc_get_dates_comma_string_localized( implode( ',', $dates_ymd_arr ) ); // Comma separated dates: '01 Jan 2023, 02 Jan 2023, ... 30 Jan 2023'
244 }
245
246
247 $readable_dates_html = '';
248
249 $readable_dates_html .= ( ! empty( $params['date_label'] ) )
250 ? $params['date_label'] . ': '
251 : '';
252
253 $readable_dates_html .= ( $params['is_in_html'] )
254 ? '<strong>' . $dates_view . '</strong>'
255 : $dates_view;
256
257 return $readable_dates_html;
258 }
259
260
261
262
263 /**
264 * Get readable dates for showing text to Users.
265 *
266 * @param $dates_ymd_arr [ '2023-10-11', '2023-11-12', '2023-11-13' ]
267 * @param $times_arr_his [ "16:00:01", "18:00:02" ]
268 * @param $params [ ] Optional
269 *
270 * @return string
271 */
272 function wpbc_get_redable_times( $dates_ymd_arr , $times_arr_his, $params = array() ){
273
274 $defaults = array(
275 'is_use_booking_recurrent_time' => ( 'On' === get_bk_option( 'booking_recurrent_time' ) ),
276 'output_dates' => get_bk_option( 'booking_date_view_type' ), // 'short' | 'wide'
277 'times_label' => ( wpbc_is_multiple_dates( $dates_ymd_arr ) ) ? __( 'Dates', 'booking' ) : __( 'Date', 'booking' ),
278 'is_in_html' => true
279 );
280 $params = wp_parse_args( $params, $defaults );
281
282 if ( wpbc_is_times_used_as_timeslots( $dates_ymd_arr ) ){
283 $readable_times_html = __( 'Time', 'booking' ) . ': <strong>' . wpbc_time_localized( $times_arr_his[0] ) . ' - ' . wpbc_time_localized( $times_arr_his[1] ) . '</strong>';
284 } else {
285 $readable_times_html = __( 'Check in', 'booking' ) . ': <strong>' . wpbc_time_localized( $times_arr_his[0] ) . '</strong>' . ' '
286 . __( 'Check out', 'booking' ) . ': <strong>' . wpbc_time_localized( $times_arr_his[1] ) . '</strong>';
287 }
288
289 return $readable_times_html;
290
291
292 // $readable_times_html = '';
293 //
294 // $readable_times_html .= ( ! empty( $params['date_label'] ) )
295 // ? $params['date_label'] . ': '
296 // : '';
297 //
298 // $readable_times_html .= ( $params['is_in_html'] )
299 // ? '<strong>' . $dates_view . '</strong>'
300 // : $dates_view;
301 //
302 // return $readable_times_html;
303 }
304
305
306
307 /**
308 * Get days in short format view
309 *
310 * @param string $sql_dates_str Dates: 2023-10-12, 2023-10-13, 2023-10-14
311 *
312 * @return string Dates in format: 2023-10-12 - 2023-10-14
313 */
314 function wpbc_get_dates_short_format( $sql_dates_str ) { // $days - string with comma seperated dates
315
316 if ( empty( $sql_dates_str ) ) {
317 return '';
318 }
319
320 $sql_dates_arr = explode( ',', $sql_dates_str );
321 $previos_date = '';
322 $short_dates_arr = array();
323
324 foreach ( $sql_dates_arr as $date_sql ) {
325
326 $date_sql = trim( $date_sql );
327
328 if ( empty( $previos_date ) ) {
329
330 // 1st date
331 $short_dates_arr[] = wpbc_get_dates_comma_string_localized( $date_sql ); // Readable date format
332
333 } else {
334
335 // 2nd and more
336 //if ( wpbc_is_next_day( $date_sql, $previos_date ) ) {
337 $next_day_if__check_in__then__check_out = true;
338 if ( wpbc_is_less_than_next_day( $date_sql, $previos_date, $next_day_if__check_in__then__check_out ) ) {
339
340 if ( ' - ' != $short_dates_arr[ ( count( $short_dates_arr ) - 1 ) ] ){
341 $short_dates_arr[] = ' - ';
342 }
343
344 } else {
345
346 if ( ' - ' == $short_dates_arr[ ( count( $short_dates_arr ) - 1 ) ] ){
347
348 $short_dates_arr[] = wpbc_get_dates_comma_string_localized( $previos_date );
349 }
350
351 $short_dates_arr[] = ', ';
352 $short_dates_arr[] = wpbc_get_dates_comma_string_localized( $date_sql ); // Readable date format
353 }
354 }
355
356 $previos_date = $date_sql;
357 }
358
359 if ( ' - ' == $short_dates_arr[ ( count( $short_dates_arr ) - 1 ) ] ){
360 $short_dates_arr[] = wpbc_get_dates_comma_string_localized( $previos_date );
361 }
362
363 $result_string = implode( '', $short_dates_arr );
364
365 return $result_string;
366 }
367
368
369
370 // ---------------------------------------------------------------------------------------------------------------------
371 // Dates Math
372 // ---------------------------------------------------------------------------------------------------------------------
373
374 /**
375 * Is multiple dates
376 *
377 * @param $dates_ymd_arr [ '2023-10-11', '2023-11-12', '2023-11-13' ]
378 *
379 * @return bool
380 */
381 function wpbc_is_multiple_dates( $dates_ymd_arr ) {
382
383 return (bool) ( count( $dates_ymd_arr ) );
384 }
385
386
387 /**
388 * Is times use as 'TIME-SLOT' or 'CHECK IN/OUT' [start/end time] for first and last dates ?
389 *
390 * @param $dates_ymd_arr [ '2023-10-11', '2023-11-12', '2023-11-13' ]
391 *
392 * @return bool
393 */
394 function wpbc_is_times_used_as_timeslots( $dates_ymd_arr ) {
395
396 if ( ( count( $dates_ymd_arr ) > 1 ) && ( 'On' != get_bk_option( 'booking_recurrent_time' ) ) ) {
397 return false; // Check In/Out
398 } else {
399 return true; // Time Slots
400 }
401 }
402
403
404 /**
405 * Check if $now_date_sql is TOMORROW of $previous_date_sql
406 *
407 * @param string $now_date_sql : '2015-02-29' | '2015-02-29 00:00:00'
408 * @param string $previous_date_sql : '2015-02-30' | '2015-02-30 00:00:00'
409 *
410 * @return boolean : true | false
411 */
412 function wpbc_is_next_day( $now_date_sql, $previous_date_sql ) {
413
414 if ( empty( $previous_date_sql ) ) {
415 return false;
416 }
417
418 $now_date_sql = wpbc_date_sql__ymd_his__to__ymd_000( $now_date_sql );
419 $previous_date_sql = wpbc_date_sql__ymd_his__to__ymd_000( $previous_date_sql );
420
421 $is_next_date = ( strtotime( $now_date_sql ) == strtotime( '+1 day', strtotime( $previous_date_sql ) ) );
422
423 return $is_next_date;
424 }
425
426 /**
427 * Check if $now_date_sql is TOMORROW of $previous_date_sql
428 *
429 * @param string $now_date_sql : '2015-02-29' | '2015-02-29 00:00:00'
430 * @param string $previous_date_sql : '2015-02-30' | '2015-02-30 00:00:00'
431 *
432 * @return boolean : true | false
433 */
434 function wpbc_is_less_than_next_day( $now_date_sql, $previous_date_sql, $next_day_if__check_in__then__check_out = false ) {
435
436 if ( empty( $previous_date_sql ) ) {
437 return false;
438 }
439
440 $now_date_sql = wpbc_date_sql__ymd_his__to__ymd_000( $now_date_sql );
441 $previous_date_sql = wpbc_date_sql__ymd_his__to__ymd_000( $previous_date_sql );
442
443 $is_next_date = ( strtotime( $now_date_sql ) <= strtotime( '+1 day', strtotime( $previous_date_sql ) ) );
444
445 // One exception for dates with check in/out times. For example '2023-10-13 13:00:01' <=> '2023-10-13 10:00:02'
446 // return false,
447 // because it's check in/out dates for the dif bookings or "recurrent times"
448 if ( ( $is_next_date )
449 && ( $next_day_if__check_in__then__check_out )
450 && ( '1' == substr( $now_date_sql, 18 ) )
451 && ( '2' == substr( $previous_date_sql, 18 ) )
452 ){
453 $is_next_date = false;
454 }
455
456
457 return $is_next_date;
458 }
459
460
461 /**
462 * Convert SQL date '2023-10-12 14:40:01' -> '2023-10-12 00:00:00'
463 *
464 * @param $sql_date '2023-10-12 14:40:01'
465 *
466 * @return string '2023-10-12 00:00:00'
467 */
468 function wpbc_date_sql__ymd_his__to__ymd_000( $sql_date ) {
469
470 $sql_date = trim( $sql_date );
471
472 $date_arr = explode( ' ', $sql_date );
473
474 return $date_arr[0] . ' 00:00:00';
475 }
476
477
478
479
480 /**
481 * Get weekday NUM from date_sql_str '2023-10-26' -> 4
482 *
483 * @param $date_sql_str
484 *
485 * @return int
486 */
487 function wpbc_date_get_week_day_num( $date_sql_str ) {
488
489 return intval( date( 'w', strtotime( $date_sql_str ) ) );
490 }
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505 // ---------------------------------------------------------------------------------------------------------------------
506 // Dates Debug
507 // ---------------------------------------------------------------------------------------------------------------------
508
509 //TODO: replace functions like this: date_i18n( 'Y-m-d H:i:s' ... to wpbc_datetime_localized( date( 'Y-m-d H:i:s',
510
511 // date_default_timezone_set( 'Europe/Amsterdam' ); // For Debug in Booking Calendar - booking form, for shortcode [wpbc_test_dates_functions] this line can be commented
512
513 // <editor-fold defaultstate="collapsed" desc=" == [wpbc_test_dates_functions] == " >
514 //TODO: delete it in version 9.9 or later, if no errors
515 /**
516 * Shortcode [wpbc_test_dates_functions] to test different dates functions on the server.
517 *
518 * @return void
519 */
520 function wpbc_test_dates_functions() {
521
522 if ( wpbc_is_on_edit_page() ) {
523 return wpbc_get_preview_for_shortcode( 'wpbc_test_dates_functions', array() ); //FixIn: 9.9.0.39
524 }
525
526 ob_start();
527 ob_clean();
528
529 $server_zone = date_default_timezone_get();
530 debuge( array( 'init' => 'Default Timezones', 'server' => date_default_timezone_get(), 'wordpress' => wp_timezone()->getName(), 'wordpress - gmt_offset' => get_option( 'gmt_offset' ) ) );
531
532 // date_default_timezone_set( 'Europe/Oslo' );
533 // date_default_timezone_set( 'Europe/Amsterdam' );
534 // date_default_timezone_set( 'UTC' );
535
536 debuge( array( 'init' => 'After WPBC changed timezone to UTC', 'server' => date_default_timezone_get(), 'wordpress' => wp_timezone()->getName(), 'wordpress - gmt_offset' => get_option( 'gmt_offset' ) ) );
537 echo '<hr>';
538 echo '<h1>Test dates functions: </h1>';
539
540
541 echo '<hr>';
542
543 for ( $i = 0; $i < 366; $i ++ ) {
544
545 $ts = date( 'Y-m-d 16:00:01', strtotime( '+' . $i . ' days' ) );
546
547 $ts_arr = explode(' ',$ts);
548
549
550
551 $date_arr = array();
552 $date_arr['str'] = '+' . $i . ' days for ' . $ts_arr[0] . ' ' .$ts_arr[1] ;
553
554 $date_arr['server'] = date_default_timezone_get();
555 $date_arr['wordpress'] = wp_timezone()->getName();
556
557 $date_arr['loc_DT'] = wpbc_datetime_localized( $ts );
558 $date_arr['loc_D'] = wpbc_date_localized( $ts );
559 $date_arr['loc_T'] = wpbc_time_localized( $ts );
560
561 $date_arr['loc_D_param_D'] = wpbc_date_localized( $ts_arr[0] );
562 $date_arr['loc_T_param_T'] = wpbc_time_localized( $ts_arr[1] );
563
564
565
566 echo '<pre><code>' . var_export( $date_arr, 1 ) . '</code></pre><br>';
567 }
568
569 date_default_timezone_set( $server_zone );
570
571 debuge( array( 'end' => 'Back to default previos timezone', 'server' => date_default_timezone_get(), 'wordpress' => wp_timezone_string() ) );
572
573 $content = ob_get_contents();
574 ob_end_clean();
575
576 return $content;
577 }
578 add_shortcode( 'wpbc_test_dates_functions', 'wpbc_test_dates_functions' );
579 // </editor-fold>
580
581 //TODO: it's only for Debug: Comment it. //FixIn: 9.9.0.17
582 // date_default_timezone_set( 'America/Chicago' ); // For Debug in Booking Calendar - booking form, for shortcode [wpbc_test_dates_functions] this line can be commented
583