PluginProbe
Booking Calendar / 11.2
Booking Calendar v11.2
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 10.11.3 10.11.4 10.12.0 10.12.1 All 199 releases
booking / core / wpbc_functions_dates.php

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

767 lines 29.3 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 /**
7 * Convert a CSV of numeric date parts to an array of integers.
8 * Example: '2025,09,05,08,55' -> [2025, 9, 5, 8, 55]
9 *
10 * @param string $csv E.g. '2025,09,05,08,55'.
11 * @param string $separator Defaults to ','.
12 *
13 * @return int[] Numeric parts as integers.
14 */
15 function wpbc_csv_numbers_to_int_array( $csv, $separator = ',' ) {
16
17 $parts = array_map( 'trim', explode( $separator, (string) $csv ) );
18
19 return array_map( 'intval', $parts );
20 }
21
22 // ---------------------------------------------------------------------------------------------------------------------
23 // Localize dates
24 // ---------------------------------------------------------------------------------------------------------------------
25
26
27 /**
28 * Return date / time in 'LOCAL_FORMAT'.
29 *
30 * @param string|int $date_str_ymdhis Date to format.
31 * @param string $format Optional. Date/Time Format, like 'Y-m-d H:i:s'
32 * @param bool $is_add_timezone_offset Optional. Timezone offset.
33 *
34 * @return string
35 */
36 function wpbc_datetime_localized( $date_str_ymdhis, $format = '', $is_add_timezone_offset = false ) {
37 // FixIn: 10.15.5.3.
38 if ( empty( $format ) ) {
39 $format = sprintf( '%s %s', get_option( 'booking_date_format' ), get_option( 'booking_time_format' ) );
40 }
41 if ( empty( $format ) ) {
42 $format = sprintf( '%s %s', get_option( 'date_format' ), get_option( 'time_format' ) );
43 }
44
45 $server_zone = date_default_timezone_get(); // If in 'Theme' or 'other plugin' set default timezone other than UTC. Save it.
46 if ( 'UTC' !== $server_zone ) { // Needed for WP date functions - set timezone to UTC
47 // phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
48 @date_default_timezone_set( 'UTC' );
49 }
50
51 if ( is_string( $date_str_ymdhis ) ) {
52 $date_str_ymdhis = strtotime( $date_str_ymdhis );
53 }
54
55 if ( false === $date_str_ymdhis ) {
56 if ( 'UTC' !== $server_zone ) {
57 // phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
58 @date_default_timezone_set( $server_zone );
59 }
60 return '';
61 }
62
63 $date_str_ymdhis = (int) $date_str_ymdhis;
64
65 if ( $is_add_timezone_offset ) {
66 $gmt_offset = get_option( 'gmt_offset', 0 );
67 $gmt_offset = is_numeric( $gmt_offset ) ? (float) $gmt_offset : 0.0;
68
69 $date_str_ymdhis += (int) round( $gmt_offset * HOUR_IN_SECONDS );
70 }
71
72 $local_date = date_i18n( $format, $date_str_ymdhis );
73
74 if ( 'UTC' !== $server_zone ) { // Back to previos state, if it was changed.
75 // phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
76 @date_default_timezone_set( $server_zone );
77 }
78
79 return $local_date;
80 }
81
82 // -----------------------------------------------------------------------------------------------------------------
83
84 // FixIn: 9.9.0.17.
85 /**
86 * Return date / time in 'LOCAL_FORMAT' with WordPress Timezone offset from WordPress > Settings General page
87 *
88 * @param string|int $date_str_ymdhis Date to format.
89 * @param string $format Optional. Date/Time Format, like 'Y-m-d H:i:s'
90 *
91 * @return string
92 */
93 function wpbc_datetime_localized__use_wp_timezone( $date_str_ymdhis, $format = '') {
94
95 $is_add_timezone_offset = true;
96
97 $datetime_localized = wpbc_datetime_localized( $date_str_ymdhis, $format, $is_add_timezone_offset );
98
99 return $datetime_localized;
100 }
101
102 /**
103 * Return date / time in 'LOCAL_FORMAT' without WordPress Timezone offset - NO TIMEZONE
104 *
105 * @param string|int $date_str_ymdhis Date to format.
106 * @param string $format Optional. Date/Time Format, like 'Y-m-d H:i:s'
107 *
108 * @return string
109 */
110 function wpbc_datetime_localized__no_wp_timezone( $date_str_ymdhis, $format = '') {
111
112 $is_add_timezone_offset = false;
113
114 $datetime_localized = wpbc_datetime_localized( $date_str_ymdhis, $format, $is_add_timezone_offset );
115
116 return $datetime_localized;
117 }
118
119
120 // -----------------------------------------------------------------------------------------------------------------
121
122 /**
123 * Return date / time in 'LOCAL_FORMAT' with WordPress Timezone offset from WordPress > Settings General page,
124 * Better use: wpbc_datetime_localized__use_wp_timezone()
125 *
126 * @param string $format Optional. Date/Time Format, like 'Y-m-d H:i:s'
127 * @param string|int $date_str_ymdhis Date to format, e.g.: '2025-01-25 12:27:20'
128 *
129 * @return string
130 */
131 function wpbc_datetime__use_wp_timezone( $format, $date_str_ymdhis = '' ) {
132
133 if ( '' === $date_str_ymdhis ) {
134 $date_str_ymdhis = gmdate( 'Y-m-d H:i:s', strtotime( 'now' ) );
135 }
136
137 return wpbc_datetime_localized__use_wp_timezone( $date_str_ymdhis, $format );
138 }
139
140
141 /**
142 * Return date / time in 'LOCAL_FORMAT' without WordPress Timezone offset - NO TIMEZONE
143 * Better use: wpbc_datetime_localized__no_wp_timezone()
144 *
145 * @param string $format Optional. Date/Time Format, like 'Y-m-d H:i:s'
146 * @param string|int $date_str_ymdhis Date to format, e.g.: '2025-01-25 12:27:20'
147 *
148 * @return string
149 */
150 function wpbc_datetime__no_wp_timezone( $format, $date_str_ymdhis = '' ) {
151
152 if ( '' === $date_str_ymdhis ) {
153 $date_str_ymdhis = gmdate( 'Y-m-d H:i:s', strtotime( 'now' ) );
154 }
155
156 return wpbc_datetime_localized__no_wp_timezone( $date_str_ymdhis, $format );
157 }
158
159
160 // -----------------------------------------------------------------------------------------------------------------
161
162
163 /**
164 * Return date in 'LOCAL_FORMAT'.
165 *
166 * @param string|int $date_str_ymd Date to format.
167 * @param string $format Optional. Date/Time Format, like 'Y-m-d'
168 * @param bool $is_add_timezone_offset Optional. Timezone offset.
169 *
170 * @return string
171 */
172 function wpbc_date_localized( $date_str_ymd, $format = '', $is_add_timezone_offset = false ) {
173
174 if ( empty( $format ) ) {
175 $format = get_option( 'booking_date_format' );
176 }
177 if ( $format === '' ) {
178 $format = get_option( 'date_format' );
179 }
180
181 return wpbc_datetime_localized( $date_str_ymd, $format, $is_add_timezone_offset );
182 }
183
184 /**
185 * Return date in 'LOCAL_FORMAT'.
186 *
187 * @param string|int $time_str_his Date to format.
188 * @param string $format Optional. Date/Time Format, like 'Y-m-d'
189 * @param bool $is_add_timezone_offset Optional. Timezone offset.
190 *
191 * @return string
192 */
193 function wpbc_time_localized( $time_str_his, $format = '', $is_add_timezone_offset = false ) {
194
195 if ( empty( $format ) ) {
196 $format = get_option( 'booking_time_format' );
197 }
198 if ( $format === '' ) {
199 $format = get_option( 'time_format' );
200 }
201
202 return wpbc_datetime_localized( $time_str_his, $format, $is_add_timezone_offset );
203 }
204
205
206 // ---------------------------------------------------------------------------------------------------------------------
207 // Localize support
208 // ---------------------------------------------------------------------------------------------------------------------
209
210 /**
211 * Get localized Dates as comma separated string from SQL comma separated Dates
212 *
213 * @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'
214 *
215 * @return string - localized comma separated Dates string
216 */
217 function wpbc_get_dates_comma_string_localized( $dates_str_in_sql_format ) {
218
219 if ( empty( $dates_str_in_sql_format ) ) {
220 return '';
221 }
222
223 $dates_array_in_sql_format = explode( ',', $dates_str_in_sql_format );
224
225 $is_no_date = apply_filters( 'wpbc_maybe_no_booking_date', false, $dates_array_in_sql_format );
226 if ( $is_no_date ) {
227 return '---';
228 }
229
230 $result_dates_arr = array();
231
232 foreach ( $dates_array_in_sql_format as $date_sql ) {
233
234 $date_sql = trim( $date_sql );
235
236 $date_arr = explode( ' ', $date_sql );
237
238 if ( count( $date_arr ) > 1 ) {
239 $time_arr = explode( ':', $date_arr[1] );
240 } else {
241 $time_arr = array( '00', '00', '00' );
242 }
243
244 if ( $time_arr == array( '00', '00', '00' ) ) {
245 $result_dates_arr[] = wpbc_date_localized( $date_sql ); // Only Date
246 } else {
247 $result_dates_arr[] = wpbc_datetime_localized( $date_sql ); // Date and Time
248 }
249 }
250
251 $mydates_result = implode( ', ', $result_dates_arr );
252
253 return $mydates_result;
254 }
255
256
257 // ---------------------------------------------------------------------------------------------------------------------
258 // Readable dates
259 // ---------------------------------------------------------------------------------------------------------------------
260
261 /**
262 * Get readable dates for showing text to Users.
263 *
264 * @param $dates_ymd_arr [ '2023-10-11', '2023-11-12', '2023-11-13' ]
265 * @param $params [ ] Optional
266 *
267 * @return string
268 */
269 function wpbc_get_redable_dates( $dates_ymd_arr , $params = array() ){
270
271 $defaults = array(
272 'is_use_booking_recurrent_time' => ( 'On' === get_bk_option( 'booking_recurrent_time' ) ),
273 'output_dates' => get_bk_option( 'booking_date_view_type' ), // 'short' | 'wide'
274 'date_label' => ( wpbc_is_multiple_dates( $dates_ymd_arr ) ) ? __( 'Dates', 'booking' ) : __( 'Date', 'booking' ),
275 'is_in_html' => true
276 );
277 $params = wp_parse_args( $params, $defaults );
278
279 if ( 'short' == $params['output_dates'] ) {
280 $dates_view = wpbc_get_dates_short_format( implode( ',', $dates_ymd_arr ) ); // Short dates: '01 Jan 2023 - 30 Jan 2023'
281 } else {
282 $dates_view = wpbc_get_dates_comma_string_localized( implode( ',', $dates_ymd_arr ) ); // Comma separated dates: '01 Jan 2023, 02 Jan 2023, ... 30 Jan 2023'
283 }
284
285
286 $readable_dates_html = '';
287
288 $readable_dates_html .= ( ! empty( $params['date_label'] ) )
289 ? $params['date_label'] . ': '
290 : '';
291
292 $readable_dates_html .= ( $params['is_in_html'] )
293 ? '<strong>' . $dates_view . '</strong>'
294 : $dates_view;
295
296 return $readable_dates_html;
297 }
298
299
300
301
302 /**
303 * Get readable dates for showing text to Users.
304 *
305 * @param $dates_ymd_arr [ '2023-10-11', '2023-11-12', '2023-11-13' ]
306 * @param $times_arr_his [ "16:00:01", "18:00:02" ]
307 * @param $params [ ] Optional
308 *
309 * @return string
310 */
311 function wpbc_get_redable_times( $dates_ymd_arr , $times_arr_his, $params = array() ){
312
313 $defaults = array(
314 'is_use_booking_recurrent_time' => ( 'On' === get_bk_option( 'booking_recurrent_time' ) ),
315 'output_dates' => get_bk_option( 'booking_date_view_type' ), // 'short' | 'wide'
316 'times_label' => ( wpbc_is_multiple_dates( $dates_ymd_arr ) ) ? __( 'Dates', 'booking' ) : __( 'Date', 'booking' ),
317 'is_in_html' => true
318 );
319 $params = wp_parse_args( $params, $defaults );
320
321 if ( wpbc_is_times_used_as_timeslots( $dates_ymd_arr ) ){
322 $readable_times_html = __( 'Time', 'booking' ) . ': <strong>' . wpbc_time_localized( $times_arr_his[0] ) . ' - ' . wpbc_time_localized( $times_arr_his[1] ) . '</strong>';
323 } else {
324 $readable_times_html = __( 'Check in', 'booking' ) . ': <strong>' . wpbc_time_localized( $times_arr_his[0] ) . '</strong>' . ' '
325 . __( 'Check out', 'booking' ) . ': <strong>' . wpbc_time_localized( $times_arr_his[1] ) . '</strong>';
326 }
327
328 return $readable_times_html;
329
330
331 // $readable_times_html = '';
332 //
333 // $readable_times_html .= ( ! empty( $params['date_label'] ) )
334 // ? $params['date_label'] . ': '
335 // : '';
336 //
337 // $readable_times_html .= ( $params['is_in_html'] )
338 // ? '<strong>' . $dates_view . '</strong>'
339 // : $dates_view;
340 //
341 // return $readable_times_html;
342 }
343
344
345
346 /**
347 * Get days in short format view
348 *
349 * @param string $sql_dates_str Dates: 2023-10-12, 2023-10-13, 2023-10-14
350 *
351 * @return string Dates in format: 2023-10-12 - 2023-10-14
352 */
353 function wpbc_get_dates_short_format( $sql_dates_str ) { // $days - string with comma seperated dates
354
355 if ( empty( $sql_dates_str ) ) {
356 return '';
357 }
358
359 $sql_dates_arr = explode( ',', $sql_dates_str );
360 $previos_date = '';
361 $short_dates_arr = array();
362
363 foreach ( $sql_dates_arr as $date_sql ) {
364
365 $date_sql = trim( $date_sql );
366
367 if ( empty( $previos_date ) ) {
368
369 // 1st date
370 $short_dates_arr[] = wpbc_get_dates_comma_string_localized( $date_sql ); // Readable date format
371
372 } else {
373
374 // 2nd and more
375 //if ( wpbc_is_next_day( $date_sql, $previos_date ) ) {
376 $next_day_if__check_in__then__check_out = true;
377 if ( wpbc_is_less_than_next_day( $date_sql, $previos_date, $next_day_if__check_in__then__check_out ) ) {
378
379 if ( ' - ' != $short_dates_arr[ ( count( $short_dates_arr ) - 1 ) ] ){
380 $short_dates_arr[] = ' - ';
381 }
382
383 } else {
384
385 if ( ' - ' == $short_dates_arr[ ( count( $short_dates_arr ) - 1 ) ] ){
386
387 $short_dates_arr[] = wpbc_get_dates_comma_string_localized( $previos_date );
388 }
389
390 $short_dates_arr[] = ', ';
391 $short_dates_arr[] = wpbc_get_dates_comma_string_localized( $date_sql ); // Readable date format
392 }
393 }
394
395 $previos_date = $date_sql;
396 }
397
398 if ( ' - ' == $short_dates_arr[ ( count( $short_dates_arr ) - 1 ) ] ){
399 $short_dates_arr[] = wpbc_get_dates_comma_string_localized( $previos_date );
400 }
401
402 $result_string = implode( '', $short_dates_arr );
403
404 return $result_string;
405 }
406
407
408
409 // ---------------------------------------------------------------------------------------------------------------------
410 // Dates Math
411 // ---------------------------------------------------------------------------------------------------------------------
412
413 /**
414 * Is multiple dates
415 *
416 * @param $dates_ymd_arr [ '2023-10-11', '2023-11-12', '2023-11-13' ]
417 *
418 * @return bool
419 */
420 function wpbc_is_multiple_dates( $dates_ymd_arr ) {
421
422 return (bool) ( count( $dates_ymd_arr ) );
423 }
424
425
426 /**
427 * Is times use as 'TIME-SLOT' or 'CHECK IN/OUT' [start/end time] for first and last dates ?
428 *
429 * @param $dates_ymd_arr [ '2023-10-11', '2023-11-12', '2023-11-13' ]
430 *
431 * @return bool
432 */
433 function wpbc_is_times_used_as_timeslots( $dates_ymd_arr ) {
434
435 if ( ( count( $dates_ymd_arr ) > 1 ) && ( 'On' != get_bk_option( 'booking_recurrent_time' ) ) ) {
436 return false; // Check In/Out
437 } else {
438 return true; // Time Slots
439 }
440 }
441
442
443 /**
444 * Check if $now_date_sql is TOMORROW of $previous_date_sql
445 *
446 * @param string $now_date_sql : '2015-02-29' | '2015-02-29 00:00:00'
447 * @param string $previous_date_sql : '2015-02-30' | '2015-02-30 00:00:00'
448 *
449 * @return boolean : true | false
450 */
451 function wpbc_is_next_day( $now_date_sql, $previous_date_sql ) {
452
453 if ( empty( $previous_date_sql ) ) {
454 return false;
455 }
456
457 $now_date_sql = wpbc_date_sql__ymd_his__to__ymd_000( $now_date_sql );
458 $previous_date_sql = wpbc_date_sql__ymd_his__to__ymd_000( $previous_date_sql );
459
460 $is_next_date = ( strtotime( $now_date_sql ) == strtotime( '+1 day', strtotime( $previous_date_sql ) ) );
461
462 return $is_next_date;
463 }
464
465 /**
466 * Check if $now_date_sql is TOMORROW of $previous_date_sql
467 *
468 * @param string $now_date_sql : '2015-02-29' | '2015-02-29 00:00:00'
469 * @param string $previous_date_sql : '2015-02-30' | '2015-02-30 00:00:00'
470 *
471 * @return boolean : true | false
472 */
473 function wpbc_is_less_than_next_day( $now_date_sql, $previous_date_sql, $next_day_if__check_in__then__check_out = false ) {
474
475 if ( empty( $previous_date_sql ) ) {
476 return false;
477 }
478
479 $now_date_sql = wpbc_date_sql__ymd_his__to__ymd_000( $now_date_sql );
480 $previous_date_sql = wpbc_date_sql__ymd_his__to__ymd_000( $previous_date_sql );
481
482 $is_next_date = ( strtotime( $now_date_sql ) <= strtotime( '+1 day', strtotime( $previous_date_sql ) ) );
483
484 // One exception for dates with check in/out times. For example '2023-10-13 13:00:01' <=> '2023-10-13 10:00:02'
485 // return false,
486 // because it's check in/out dates for the dif bookings or "recurrent times"
487 if ( ( $is_next_date )
488 && ( $next_day_if__check_in__then__check_out )
489 && ( '1' == substr( $now_date_sql, 18 ) )
490 && ( '2' == substr( $previous_date_sql, 18 ) )
491 ){
492 $is_next_date = false;
493 }
494
495
496 return $is_next_date;
497 }
498
499
500 /**
501 * Check if $date_sql1 < $date_sql2
502 *
503 * @param string $date_sql1 : '2015-02-29' | '2015-02-29 00:00:00'
504 * @param string $date_sql2 : '2015-02-30' | '2015-02-30 00:00:00'
505 *
506 * @return boolean : true | false
507 */
508 function wpbc_is_date_less_than( $date_sql1, $date_sql2 ) {
509
510 if ( empty( $date_sql2 ) ) {
511 return false;
512 }
513
514 $date_sql1 = wpbc_date_sql__ymd_his__to__ymd_000( $date_sql1 );
515 $date_sql2 = wpbc_date_sql__ymd_his__to__ymd_000( $date_sql2 );
516
517 $is_date_less = ( strtotime( $date_sql1 ) < strtotime( $date_sql2 ) );
518
519 return $is_date_less;
520 }
521
522
523 /**
524 * Check if $date_sql1 > $date_sql2
525 *
526 * @param string $date_sql1 : '2015-02-29' | '2015-02-29 00:00:00'
527 * @param string $date_sql2 : '2015-02-30' | '2015-02-30 00:00:00'
528 *
529 * @return boolean : true | false
530 */
531 function wpbc_is_date_higher_than( $date_sql1, $date_sql2 ) {
532
533 if ( empty( $date_sql2 ) ) {
534 return false;
535 }
536
537 $date_sql1 = wpbc_date_sql__ymd_his__to__ymd_000( $date_sql1 );
538 $date_sql2 = wpbc_date_sql__ymd_his__to__ymd_000( $date_sql2 );
539
540 $is_date_less = ( strtotime( $date_sql1 ) > strtotime( $date_sql2 ) );
541
542 return $is_date_less;
543 }
544
545
546 /**
547 * Convert SQL date '2023-10-12 14:40:01' -> '2023-10-12 00:00:00'
548 *
549 * @param $sql_date '2023-10-12 14:40:01'
550 *
551 * @return string '2023-10-12 00:00:00'
552 */
553 function wpbc_date_sql__ymd_his__to__ymd_000( $sql_date ) {
554
555 $sql_date = trim( $sql_date );
556
557 $date_arr = explode( ' ', $sql_date );
558
559 return $date_arr[0] . ' 00:00:00';
560 }
561
562
563
564
565 /**
566 * Get weekday NUM from date_sql_str '2023-10-26' -> 4
567 *
568 * @param $date_sql_str
569 *
570 * @return int
571 */
572 function wpbc_date_get_week_day_num( $date_sql_str ) {
573
574 return intval( gmdate( 'w', strtotime( $date_sql_str ) ) );
575 }
576
577
578
579
580 // ---------------------------------------------------------------------------------------------------------------------
581 // Dates Debug
582 // ---------------------------------------------------------------------------------------------------------------------
583
584 //TODO: replace functions like this: date_i18n( 'Y-m-d H:i:s' ... to wpbc_datetime_localized( gmdate( 'Y-m-d H:i:s',
585 // phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
586 // date_default_timezone_set( 'Europe/Amsterdam' ); // For Debug in Booking Calendar - booking form, for shortcode [wpbc_test_dates_functions] this line can be commented
587
588 // <editor-fold defaultstate="collapsed" desc=" == [wpbc_test_dates_functions] == " >
589 //TODO: delete it in version 9.9 or later, if no errors
590 /**
591 * Shortcode [wpbc_test_dates_functions] to test different dates functions on the server.
592 *
593 * @return void
594 */
595 function wpbc_test_dates_functions() {
596
597 if ( wpbc_is_on_edit_page() ) {
598 return wpbc_get_preview_for_shortcode( 'wpbc_test_dates_functions', array() ); // FixIn: 9.9.0.39.
599 }
600
601 ob_start();
602 ob_clean();
603
604 $server_zone = date_default_timezone_get();
605 debuge( array( 'init' => 'Default Timezones', 'server' => date_default_timezone_get(), 'wordpress' => wp_timezone()->getName(), 'wordpress - gmt_offset' => get_option( 'gmt_offset' ) ) );
606 // phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
607 // date_default_timezone_set( 'Europe/Oslo' );
608 // date_default_timezone_set( 'Europe/Amsterdam' );
609 // date_default_timezone_set( 'UTC' );
610
611 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' ) ) );
612 echo '<hr>';
613 echo '<h1>Test dates functions: </h1>';
614
615
616 echo '<hr>';
617
618 for ( $i = 0; $i < 366; $i ++ ) {
619
620 $ts = gmdate( 'Y-m-d 16:00:01', strtotime( '+' . $i . ' days' ) );
621
622 $ts_arr = explode(' ',$ts);
623
624
625
626 $date_arr = array();
627 $date_arr['str'] = '+' . $i . ' days for ' . $ts_arr[0] . ' ' .$ts_arr[1] ;
628
629 $date_arr['server'] = date_default_timezone_get();
630 $date_arr['wordpress'] = wp_timezone()->getName();
631
632 $date_arr['loc_DT'] = wpbc_datetime_localized( $ts );
633 $date_arr['loc_D'] = wpbc_date_localized( $ts );
634 $date_arr['loc_T'] = wpbc_time_localized( $ts );
635
636 $date_arr['loc_D_param_D'] = wpbc_date_localized( $ts_arr[0] );
637 $date_arr['loc_T_param_T'] = wpbc_time_localized( $ts_arr[1] );
638
639
640 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.PHP.DevelopmentFunctions.error_log_var_export
641 echo '<pre><code>' . var_export( $date_arr, 1 ) . '</code></pre><br>';
642 }
643 // phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
644 date_default_timezone_set( $server_zone );
645
646 debuge( array( 'end' => 'Back to default previos timezone', 'server' => date_default_timezone_get(), 'wordpress' => wp_timezone_string() ) );
647
648 $content = ob_get_contents();
649 ob_end_clean();
650
651 return $content;
652 }
653 add_shortcode( 'wpbc_test_dates_functions', 'wpbc_test_dates_functions' );
654 // </editor-fold>
655
656 //TODO: it's only for Debug: Comment it. // FixIn: 9.9.0.17.
657 // phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
658 // date_default_timezone_set( 'America/Chicago' ); // For Debug in Booking Calendar - booking form, for shortcode [wpbc_test_dates_functions] this line can be commented
659
660
661 // ---------------------------------------------------------------------------------------------------------------------
662 // Get available / unavailable intervals based from Today date (current time).
663 // ---------------------------------------------------------------------------------------------------------------------
664
665 /**
666 * Get unavailable from today array values and hints
667 *
668 * @return array - [
669 * booking_unavailable_days_num_from_today = "540m"
670 * booking_unavailable_days_num_from_today__hint = ": 2025-01-25 15:19 - 2025-01-26 00:18:42"
671 * booking_available_days_num_from_today = "0"
672 * booking_available_days_num_from_today__hint = ": 26 Jan, 2025 - ..."
673 * ]
674 */
675 function wpbc_get_unavailable_from_today_hints_arr() {
676
677 $data_arr = array();
678
679 // -----------------------------------------------------------------------------------------------------
680 // == UNAVAILABLE Today days ==
681 // -----------------------------------------------------------------------------------------------------
682 // FixIn: 10.8.1.4.
683 $last_unavailable_date = '';
684 if ( 'm' === substr( get_bk_option( 'booking_unavailable_days_num_from_today' ), - 1 ) ) {
685 // -------------------------------------------------------------------------------------------------
686 // == Minutes ==
687 // -------------------------------------------------------------------------------------------------
688 $data_arr['booking_unavailable_days_num_from_today'] = intval( get_bk_option( 'booking_unavailable_days_num_from_today' ) );
689
690 // Hints.
691 $data_arr['booking_unavailable_days_num_from_today__hint'] = ': <span style="text-transform: lowercase;font-size:0.9em;">' . __( 'None', 'booking' ) . '</span>';
692 if ( ! empty( $data_arr['booking_unavailable_days_num_from_today'] ) ) {
693
694 $start_date_unix = strtotime( 'now' );
695 $todate_with_wp_timezone = wpbc_datetime_localized__use_wp_timezone( gmdate( 'Y-m-d H:i:s', $start_date_unix ), 'Y-m-d H:i' );
696
697 $start_date_unix = strtotime( '+' . ( intval( $data_arr['booking_unavailable_days_num_from_today'] ) - 1 ) . ' minutes' );
698 $max_date_with_wp_timezone = wpbc_datetime_localized__use_wp_timezone( gmdate( 'Y-m-d H:i:s', $start_date_unix ), 'Y-m-d H:i:s' );
699
700 $last_unavailable_date = $max_date_with_wp_timezone;
701
702 $data_arr['booking_unavailable_days_num_from_today__hint'] = ': <a style="color:#626262;" href="'.esc_url(get_admin_url( null, 'options-general.php#timezone_string' )).'">' . $todate_with_wp_timezone . '</a> - ' . $max_date_with_wp_timezone; // FixIn: 10.9.4.2.
703 }
704 $data_arr['booking_unavailable_days_num_from_today'] .= 'm';
705 } else {
706 // -------------------------------------------------------------------------------------------------
707 // == Days ==
708 // -------------------------------------------------------------------------------------------------
709 $data_arr['booking_unavailable_days_num_from_today'] = intval( get_bk_option( 'booking_unavailable_days_num_from_today' ) );
710
711 // Hints.
712 $data_arr['booking_unavailable_days_num_from_today__hint'] = ': <span style="text-transform: lowercase;font-size:0.9em;">' . __( 'None', 'booking' ) . '</span>';
713
714 if ( 1 === $data_arr['booking_unavailable_days_num_from_today'] ) {
715 $last_unavailable_date = wp_date( 'Y-m-d 00:00:00' );
716 $data_arr['booking_unavailable_days_num_from_today__hint'] = ': ' . wp_date( 'd M', strtotime( $last_unavailable_date ) );
717 }
718 if ( $data_arr['booking_unavailable_days_num_from_today'] > 1 ) {
719 $last_unavailable_date = wp_date( 'Y-m-d 00:00:00', strtotime( '+' . ( $data_arr['booking_unavailable_days_num_from_today'] - 1 ) . ' days' ) );
720 $data_arr['booking_unavailable_days_num_from_today__hint'] = ': ' . wp_date( 'd M' ) . ' - ' . wp_date( 'd M', strtotime( $last_unavailable_date ) );
721 }
722 }
723
724 // -----------------------------------------------------------------------------------------------------
725 // == AVAILABLE Today days ==
726 // -----------------------------------------------------------------------------------------------------
727 // if ( class_exists( 'wpdev_bk_biz_m' ) ) { .
728 $data_arr['booking_available_days_num_from_today'] = esc_js( get_bk_option( 'booking_available_days_num_from_today' ) );
729
730 // Hints.
731 // $start_available_date = ( '' === $last_unavailable_date ) ? wp_date( 'Y-m-d 00:00:00' ) : wp_date( 'Y-m-d 00:00:00', strtotime( '+1 day', strtotime( $last_unavailable_date ) ) );
732 if ( '' === $last_unavailable_date ) {
733 $start_date_unix = strtotime( 'now' );
734 $todate_with_wp_timezone = wpbc_datetime_localized__use_wp_timezone( gmdate( 'Y-m-d H:i:s', $start_date_unix ), 'Y-m-d 00:00:00' );
735 $start_available_date = $todate_with_wp_timezone;
736 } else {
737 $start_available_date = $last_unavailable_date;
738 // We use here with no WP timezone, because timezone already applied to $last_unavailable_date.
739 $start_available_date = wpbc_datetime_localized__no_wp_timezone( strtotime( $last_unavailable_date ), 'Y-m-d 00:00:00' );
740 }
741
742
743 if ( empty( $data_arr['booking_available_days_num_from_today'] ) ) {
744 $last_available_date = '';
745 } else {
746 // $last_available_date = wp_date( 'Y-m-d 00:00:00', strtotime( '+' . ( $data_arr['booking_available_days_num_from_today'] ) . ' days' ) );
747 // FixIn: 10.9.6.3.
748 $start_date_unix = strtotime( '+' . ( intval( $data_arr['booking_available_days_num_from_today'] ) - 1 ) . ' days' );
749 $last_available_date = wpbc_datetime_localized__use_wp_timezone( gmdate( 'Y-m-d H:i:s', $start_date_unix ), 'Y-m-d 00:00:00' );
750 }
751
752 if ( ! empty( $data_arr['booking_available_days_num_from_today'] ) ) {
753
754 if ( strtotime( $start_available_date ) < strtotime( $last_available_date ) ) {
755
756 $data_arr['booking_available_days_num_from_today__hint'] = ': ' . wp_date( 'd M, Y', strtotime( $start_available_date ) ) . ' - ' . wp_date( 'd M, Y', strtotime( $last_available_date ) );
757 } else if ( strtotime( $start_available_date ) == strtotime( $last_available_date ) ) {
758 $data_arr['booking_available_days_num_from_today__hint'] = ': ' . wp_date( 'd M, Y', strtotime( $start_available_date ) );
759 } else {
760 $data_arr['booking_available_days_num_from_today__hint'] = ': <span style="text-transform: uppercase;font-size:1.1em;">' . esc_html__( 'None', 'booking' ) . '</span><br>' . ' <span style="text-transform: lowercase;font-size:0.9em;color:#cc3a5f;">' . 'Start available' . ': ' . wp_date( 'd M, Y', strtotime( $start_available_date ) ) . '<br>' . 'Last available' . ': ' . wp_date( 'd M, Y', strtotime( $last_available_date ) ) . '</span>';
761 }
762 } else {
763 $data_arr['booking_available_days_num_from_today__hint'] = ': ' . wp_date( 'd M, Y', strtotime( $start_available_date ) ) . ' - ...';
764 }
765
766 return $data_arr;
767 }