[wp-trac] [WordPress Trac] #59235: AJAX request returns critical error in class-wp-date-query.php

WordPress Trac noreply at wordpress.org
Tue Aug 29 08:35:27 UTC 2023


#59235: AJAX request returns critical error in class-wp-date-query.php
-------------------------------+-----------------------------
 Reporter:  reklwera           |      Owner:  (none)
     Type:  defect (bug)       |     Status:  new
 Priority:  normal             |  Milestone:  Awaiting Review
Component:  Query              |    Version:  trunk
 Severity:  normal             |   Keywords:  has-patch
  Focuses:  php-compatibility  |
-------------------------------+-----------------------------
 This is due to the mktime gets the $year variable as string and not int.

 This is the error code given:
 {{{
 PHP Fatal error:  Uncaught TypeError: mktime(): Argument #6 ($year) must
 be of type ?int, string given in /wp-includes/class-wp-date-query.php:320
 }}}

 The code is:
 {{{#!php
 <?php
                 // Days per year.
                 if ( array_key_exists( 'year', $date_query ) ) {
                         /*
                          * If a year exists in the date query, we can use
 it to get the days.
                          * If multiple years are provided (as in a
 BETWEEN), use the first one.
                          */
                         if ( is_array( $date_query['year'] ) ) {
                                 $_year = reset( $date_query['year'] );
                         } else {
                                 $_year = $date_query['year'];
                         }

                         $max_days_of_year = gmdate( 'z', mktime( 0, 0, 0,
 12, 31, $_year ) ) + 1;
                 } else {
                         // Otherwise we use the max of 366 (leap-year).
                         $max_days_of_year = 366;
                 }
 }}}

 To fix this one can be sure to use intval:
 {{{#!php
 <?php
                 // Days per year.
                 if ( array_key_exists( 'year', $date_query ) ) {
                         /*
                          * If a year exists in the date query, we can use
 it to get the days.
                          * If multiple years are provided (as in a
 BETWEEN), use the first one.
                          */
                         if ( is_array( $date_query['year'] ) ) {
                                 $_year = intval(reset( $date_query['year']
 ));
                         } else {
                                 $_year = intval($date_query['year']);
                         }

                         $max_days_of_year = gmdate( 'z', mktime( 0, 0, 0,
 12, 31, $_year ) ) + 1;
                 } else {
                         // Otherwise we use the max of 366 (leap-year).
                         $max_days_of_year = 366;
                 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/59235>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list