Automattic / Automattic/jetpack

Milestone Widget: Suggestion for improving accuracy

Open
#18,449 2 comments 1 reaction 0 assignees View on GitHub
[Feature] Extra Sidebar Widgets [Pri] Low Enhancement Good For Community
Dominant language
PHP
Stars
1.8k
Forks
898
Avg merge
1d 18h
Merged PRs (30d)
774

Description

#### Is your feature request related to a problem? Please describe.

> The widget displays _8_days for an event to be held @ 3pm in a week (7 days from today) right up until 3pm today.

#### Describe the solution you'd like

> Please improve your automatic time units to fix this misleading round-off error: 7days + 6 hours == 7.25 days but != 8 days.

Suggested fix:

line 538 in plugins/jetpack/modules/widgets/milestone/class-milestone-widget.php
change
`return (int) ( $seconds / 60 / 60 / 24 + 1 );`
to
`return (int) ( $seconds / 60 / 60 / 24 + 0.5 );`

> (also note that all other units are cast to integer; here is my patch for simple 1/2 rounding instead of rounding up; added roundings noted by lack of space after 0.5):

```
protected function get_interval_in_units( $seconds, $units ) {
switch ( $units ) {
case 'years':
$years = $seconds / YEAR_IN_SECONDS;
$decimals = abs( round( $years, 1 ) - round( $years ) ) > 0 ? 1 : 0;
return number_format_i18n( $years, $decimals );
case 'months':
return (int) ( $seconds / 60 / 60 / 24 / 30 + 0.5);
case 'days':
return (int) ( $seconds / 60 / 60 / 24 + 0.5 );
case 'hours':
return (int) ( $seconds / 60 / 60 + 0.5);
case 'minutes':
return (int) ( $seconds / 60 + 0.5 );
default:
return $seconds;
}
}
```

#### Describe alternatives you've considered

Alternatively add a days+hours option.

#### Additional context

Reported here: https://wordpress.org/support/topic/milestone-widget-countdown-days-inaccurate/

Contributor guide

Open the contributing guide

Research direction

Start in plugins/jetpack/modules/widgets/milestone/class-milestone-widget.php at line 538 and inspect get_interval_in_units(), especially the days, months, hours, and minutes cases. Compare the current countdown output with the requested half-unit rounding and verify that the milestone widget no longer displays 8 days for an event 7 days and 6 hours away.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, wordpress
Domain
frontend, web-dev
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.