Add this note to the top of $volume_totals on all pages, then replace the @last_diff_orig with this version. See Gauges Controller for example. There is no need to have unnecessary checks. /*meters.created_date will never be null in the new db, so this case is not needed for @last_diff_orig := (CASE WHEN mtr.created_date IS NULL THEN 1 ELSE (UNIX_TIMESTAMP(mtr.created_date) - @device_commencement_date) / 60/60/24 END) last_diff_orig, @last_diff_orig := (UNIX_TIMESTAMP(mtr.created_date) - @device_commencement_date / 60/60/24) last_diff_orig,