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: |
|
Owned by: |
|
|---|---|---|---|
| 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
@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.
Trac ticket: https://core.trac.wordpress.org/ticket/65046
The variables
$mmand$mdhad their substr() positions and inlineFixes #65046
Trac ticket:
## Use of AI Tools