I'm trying to calculate whether a given vehicle's mileage is within the recommended service windows.

For example, if a fuel filter replacement is every 15,000 miles and the service window is 5,000 miles, then a vehicle is within the service window from 12,500-17,500 miles, 27,500-32,500 miles, etc.

My current logic works (I think), but I'm not sure if it's too convoluted or if there are better ways of figuring this out:

$mileage = 27500;
$service = 15000;
$window = 2500;
$is_due = $mileage < $service
    ? $service - $mileage <= $window
    : $mileage % service <= $window
        || abs($mileage % $service - $service) <= $window;
  • \$\begingroup\$ I'm a bit worried about your reasoning. Suppose we take your example about the fuel filter, and the vehicle turns up in the first service window at 13,500 miles. The filter is replaced. Would the next service window not be from 26,000 to 31,000 miles? \$\endgroup\$ Mar 17 '18 at 16:34
  • \$\begingroup\$ @KIKOSoftware You are totally right. However, the system I'm doing this for doesn't record previous services. This is more: "You might have scheduled maintenance due." \$\endgroup\$
    – Shane
    Mar 17 '18 at 21:02

This seems overly complex or not complex enough. If the fuel filter is replaced at 12,500, then the next service window should be from 25,000 to 30,000, not 27,500 to 32,500.

If the fuel filter is not replaced by 17,500, it doesn't go back into service either. It still needs replaced at 18,000, etc.

$current_mileage = 27500;
$last_service = 12500;
$service_interval = 15000;
$half_window = 2500;

$is_due = $current_mileage + $half_window <= $last_service + $service_interval;

$is_overdue = $current_mileage > $last_service + $service_interval + $half_window;

This seems to better reflect how service windows work in practice.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.