Make WordPress Core

Opened 12 days ago

Last modified 24 hours ago

#65046 assigned defect (bug)

get_weekstartend() has swapped variable names and comments for month and day

Reported by: saratheonline's profile saratheonline Owned by: saratheonline's profile saratheonline
Milestone: 7.1 Priority: normal
Severity: normal Version: trunk
Component: Date/Time Keywords: has-patch needs-testing
Focuses: sustainability, coding-standards Cc:

Description

In get_weekstartend() (src/wp-includes/functions.php), the variable names

and comments for month and day are swapped, making the code misleading and
a maintenance risk.

For a MySQL date string like "2024-12-15":

  • Position 5-6 = "12" (month)
  • Position 8-9 = "15" (day)

But the current code does the opposite:

MySQL string month.
$mm = substr( $mysqlstring, 8, 2 );
actually extracts the day

MySQL string day.
$md = substr( $mysqlstring, 5, 2 );
actually extracts the month

The result of mktime() is accidentally correct because the two errors cancel
each other out, but anyone reading or maintaining this code would be confused
— or worse, "fix" the variable names without updating mktime() and introduce
a real bug.

Proposed Fix

MySQL string month.
$mm = substr( $mysqlstring, 5, 2 );

MySQL string day.
$md = substr( $mysqlstring, 8, 2 );

The timestamp for MySQL string day.
$day = mktime( 0, 0, 0, $mm, $md, $my );

This makes the variable names, comments, substr positions, and mktime()
argument order all consistent. The output is identical.

Change History (5)

This ticket was mentioned in PR #11504 on WordPress/wordpress-develop by @saratheonline.


12 days ago
#1

Trac ticket: https://core.trac.wordpress.org/ticket/65046

The variables $mm and $md had their substr() positions and inline

comments swapped — $mm was extracting the day digits (position 8)
while $md was extracting the month digits (position 5), contrary to
what the comments indicated.


The output was accidentally correct because the two mistakes cancelled
each other out in the mktime() call, but the misleading naming posed
a future maintenance risk.


Corrects the substr() positions and mktime() argument order so that
variable names, comments, and logic are all consistent.

Fixes #65046

Trac ticket:

## Use of AI Tools

#3 @saratheonline
4 days ago

  • Focuses sustainability coding-standards added

#4 @westonruter
25 hours ago

  • Milestone changed from Awaiting Review to 7.1

@westonruter commented on PR #11504:


24 hours ago
#5

Part of the problem with the original code is the use of the very short abbreviated variables make it difficult to read and understand to begin with. Please also update the variables to use full words so that it is obvious what values they contain.

Note: See TracTickets for help on using tickets.