[wp-trac] [WordPress Trac] #61732: get_calendar() will use invalid dates in SQL queries

WordPress Trac noreply at wordpress.org
Wed Nov 6 13:30:10 UTC 2024


#61732: get_calendar() will use invalid dates in SQL queries
-------------------------------------------------+-------------------------
 Reporter:  leedxw                               |       Owner:  (none)
     Type:  defect (bug)                         |      Status:  new
 Priority:  normal                               |   Milestone:  Awaiting
                                                 |  Review
Component:  Date/Time                            |     Version:
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch needs-unit-tests needs-    |     Focuses:
  testing-info needs-testing                     |
-------------------------------------------------+-------------------------

Comment (by ngoncalves):

 I have the same issue and this is the errors I can see in logs.
 {{{
 PHP message: WordPress database error Incorrect DATETIME value:
 '2024-11-31 23:59:59' for query SELECT MONTH(post_date) AS month,
 YEAR(post_date) AS year
           FROM wp_15_posts
           WHERE post_date > '2024-11-31 23:59:59'
           AND post_type = 'post' AND post_status = 'publish'
           ORDER BY post_date ASC
           LIMIT 1 made by require('wp-blog-header.php'), require_once('wp-
 includes/template-loader.php'), include('wp-includes/template-
 canvas.php'), get_the_block_template_html, do_blocks, render_block,
 WP_Block->render, render_block_core_template_part, do_blocks,
 render_block, WP_Block->render, WP_Block->render, WP_Block->render,
 WP_Block->render, render_block_core_calendar, get_calendar, QM_DB->query
 }}}

 In the get_calendar function in wp-includes/general-template.php, before
 it tries to figure out if it should show a link to the next month with
 posts, it creates a unix timestamp of the first of the month at midnight
 in order to get the last day of that month.
 Depending on the timezone configured, this can make the date rollback to
 the last day of the previous month. This can have 2 consequences:
 - if the previous month has more days than the current month, it crashes
 with the above error.
 - if the previous month has less days than the current month, it won't
 crash but there will be weird due to not being properly clamped to the end
 of the month

 Exemple:
 {{{
  $unixmonth = mktime( 0, 0, 0, $thismonth, 1, $thisyear );
  $last_day  = gmdate( 't', $unixmonth );
 }}}
 Assuming `$thismonth = 11` and `$thisyear = 2024`,
 With Europe/Paris timezone: `$unixmonth = 1730415600 $lastday = 31`
 With UTC timezone timezone: `$unixmonth 1730419200 $lastday = 30`

 To fix that issue, creating `$unixmonth` with a day of 2 would stop any
 possibility of rollback.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/61732#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list