PluginProbe
Booking Calendar / 11.8.2
Booking Calendar v11.8.2
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
← All changes | core/wpbc_functions_dates.php +214 -29 10.1.311.8.2 View file →
@@ -1,8 +1,25 @@
1 1 <?php
2 2
3 3 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4 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 +
5 22 // ---------------------------------------------------------------------------------------------------------------------
6 23 // Localize dates
7 24 // ---------------------------------------------------------------------------------------------------------------------
8 25
@@ -16,9 +33,9 @@
16 33 *
17 34 * @return string
18 35 */
19 36 function wpbc_datetime_localized( $date_str_ymdhis, $format = '', $is_add_timezone_offset = false ) {
20 -
37 + // FixIn: 10.15.5.3.
21 38 if ( empty( $format ) ) {
22 39 $format = sprintf( '%s %s', get_option( 'booking_date_format' ), get_option( 'booking_time_format' ) );
23 40 }
24 41 if ( empty( $format ) ) {
@@ -26,8 +43,9 @@
26 43 }
27 44
28 45 $server_zone = date_default_timezone_get(); // If in 'Theme' or 'other plugin' set default timezone other than UTC. Save it.
29 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
30 48 @date_default_timezone_set( 'UTC' );
31 49 }
32 50
33 51 if ( is_string( $date_str_ymdhis ) ) {
@@ -33,15 +51,29 @@
33 51 if ( is_string( $date_str_ymdhis ) ) {
34 52 $date_str_ymdhis = strtotime( $date_str_ymdhis );
35 53 }
36 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 +
37 65 if ( $is_add_timezone_offset ) {
38 - $date_str_ymdhis += (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
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 );
39 70 }
40 71
41 72 $local_date = date_i18n( $format, $date_str_ymdhis );
42 73
43 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
44 76 @date_default_timezone_set( $server_zone );
45 77 }
46 78
47 79 return $local_date;
@@ -48,9 +80,9 @@
48 80 }
49 81
50 82 // -----------------------------------------------------------------------------------------------------------------
51 83
52 - //FixIn: 9.9.0.17
84 + // FixIn: 9.9.0.17.
53 85 /**
54 86 * Return date / time in 'LOCAL_FORMAT' with WordPress Timezone offset from WordPress > Settings General page
55 87 *
56 88 * @param string|int $date_str_ymdhis Date to format.
@@ -87,12 +119,13 @@
87 119
88 120 // -----------------------------------------------------------------------------------------------------------------
89 121
90 122 /**
91 - * Return date / time in 'LOCAL_FORMAT' with WordPress Timezone offset from WordPress > Settings General page
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()
92 125 *
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.
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'
95 128 *
96 129 * @return string
97 130 */
98 131 function wpbc_datetime__use_wp_timezone( $format, $date_str_ymdhis = '' ) {
@@ -97,9 +130,9 @@
97 130 */
98 131 function wpbc_datetime__use_wp_timezone( $format, $date_str_ymdhis = '' ) {
99 132
100 133 if ( '' === $date_str_ymdhis ) {
101 - $date_str_ymdhis = date( 'Y-m-d H:i:s', strtotime( 'now' ) );
134 + $date_str_ymdhis = gmdate( 'Y-m-d H:i:s', strtotime( 'now' ) );
102 135 }
103 136
104 137 return wpbc_datetime_localized__use_wp_timezone( $date_str_ymdhis, $format );
105 138 }
@@ -106,11 +139,12 @@
106 139
107 140
108 141 /**
109 142 * Return date / time in 'LOCAL_FORMAT' without WordPress Timezone offset - NO TIMEZONE
143 + * Better use: wpbc_datetime_localized__no_wp_timezone()
110 144 *
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.
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'
113 147 *
114 148 * @return string
115 149 */
116 150 function wpbc_datetime__no_wp_timezone( $format, $date_str_ymdhis = '' ) {
@@ -115,9 +149,9 @@
115 149 */
116 150 function wpbc_datetime__no_wp_timezone( $format, $date_str_ymdhis = '' ) {
117 151
118 152 if ( '' === $date_str_ymdhis ) {
119 - $date_str_ymdhis = date( 'Y-m-d H:i:s', strtotime( 'now' ) );
153 + $date_str_ymdhis = gmdate( 'Y-m-d H:i:s', strtotime( 'now' ) );
120 154 }
121 155
122 156 return wpbc_datetime_localized__no_wp_timezone( $date_str_ymdhis, $format );
123 157 }
@@ -187,8 +221,13 @@
187 221 }
188 222
189 223 $dates_array_in_sql_format = explode( ',', $dates_str_in_sql_format );
190 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 +
191 230 $result_dates_arr = array();
192 231
193 232 foreach ( $dates_array_in_sql_format as $date_sql ) {
194 233
@@ -458,8 +497,54 @@
458 497 }
459 498
460 499
461 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 + /**
462 547 * Convert SQL date '2023-10-12 14:40:01' -> '2023-10-12 00:00:00'
463 548 *
464 549 * @param $sql_date '2023-10-12 14:40:01'
465 550 *
@@ -485,30 +570,20 @@
485 570 * @return int
486 571 */
487 572 function wpbc_date_get_week_day_num( $date_sql_str ) {
488 573
489 - return intval( date( 'w', strtotime( $date_sql_str ) ) );
574 + return intval( gmdate( 'w', strtotime( $date_sql_str ) ) );
490 575 }
491 576
492 577
493 578
494 579
495 -
496 -
497 -
498 -
499 -
500 -
501 -
502 -
503 -
504 -
505 580 // ---------------------------------------------------------------------------------------------------------------------
506 581 // Dates Debug
507 582 // ---------------------------------------------------------------------------------------------------------------------
508 583
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 -
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
511 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
512 587
513 588 // <editor-fold defaultstate="collapsed" desc=" == [wpbc_test_dates_functions] == " >
514 589 //TODO: delete it in version 9.9 or later, if no errors
@@ -519,9 +594,9 @@
519 594 */
520 595 function wpbc_test_dates_functions() {
521 596
522 597 if ( wpbc_is_on_edit_page() ) {
523 - return wpbc_get_preview_for_shortcode( 'wpbc_test_dates_functions', array() ); //FixIn: 9.9.0.39
598 + return wpbc_get_preview_for_shortcode( 'wpbc_test_dates_functions', array() ); // FixIn: 9.9.0.39.
524 599 }
525 600
526 601 ob_start();
527 602 ob_clean();
@@ -527,9 +602,9 @@
527 602 ob_clean();
528 603
529 604 $server_zone = date_default_timezone_get();
530 605 debuge( array( 'init' => 'Default Timezones', 'server' => date_default_timezone_get(), 'wordpress' => wp_timezone()->getName(), 'wordpress - gmt_offset' => get_option( 'gmt_offset' ) ) );
531 -
606 +// phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
532 607 // date_default_timezone_set( 'Europe/Oslo' );
533 608 // date_default_timezone_set( 'Europe/Amsterdam' );
534 609 // date_default_timezone_set( 'UTC' );
535 610
@@ -541,9 +616,9 @@
541 616 echo '<hr>';
542 617
543 618 for ( $i = 0; $i < 366; $i ++ ) {
544 619
545 - $ts = date( 'Y-m-d 16:00:01', strtotime( '+' . $i . ' days' ) );
620 + $ts = gmdate( 'Y-m-d 16:00:01', strtotime( '+' . $i . ' days' ) );
546 621
547 622 $ts_arr = explode(' ',$ts);
548 623
549 624
@@ -561,12 +636,12 @@
561 636 $date_arr['loc_D_param_D'] = wpbc_date_localized( $ts_arr[0] );
562 637 $date_arr['loc_T_param_T'] = wpbc_time_localized( $ts_arr[1] );
563 638
564 639
565 -
640 + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.PHP.DevelopmentFunctions.error_log_var_export
566 641 echo '<pre><code>' . var_export( $date_arr, 1 ) . '</code></pre><br>';
567 642 }
568 -
643 + // phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
569 644 date_default_timezone_set( $server_zone );
570 645
571 646 debuge( array( 'end' => 'Back to default previos timezone', 'server' => date_default_timezone_get(), 'wordpress' => wp_timezone_string() ) );
572 647
@@ -577,6 +652,116 @@
577 652 }
578 653 add_shortcode( 'wpbc_test_dates_functions', 'wpbc_test_dates_functions' );
579 654 // </editor-fold>
580 655
581 -//TODO: it's only for Debug: Comment it. //FixIn: 9.9.0.17
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
582 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 +}