You appear to be quite close to having this all sorted out. If you wrap your queries in a function, and separate the logic and presentation:
<?php
function wpse_158425_get_terms(){
global $wpdb, $rpt_options;
$wpdb->show_errors();
$rpt_days = $rpt_options['number_days'];
$rpt_term_ids = $wpdb->get_col("
SELECT term_id FROM $wpdb->term_taxonomy
INNER JOIN $wpdb->term_relationships ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
INNER JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->term_relationships.object_id
WHERE DATE_SUB(CURDATE(), INTERVAL $rpt_days DAY) <= $wpdb->posts.post_date
");
if(count($rpt_term_ids) > 0){
$rpt_tags = get_tags(array(
'orderby' => 'count',
'order' => 'DESC',
'number' => $rpt_options['number_tags'],
'include' => $rpt_term_ids,
));
} else {
$rpt_tags = FALSE;
}
return $rpt_tags;
}
function wpse_158425_display_terms(){
global $rpt_options;
$tags = wpse_158425_get_terms();
$recentTags="<div class="recent-popular-tags">";
$recentTags .= '<h3>'.$rpt_options['heading_tags'].'</h3>';
if($tags != FALSE){
foreach($tags as $tag){
$recentTags .= '<span class="rpt-link"><a href="' . get_tag_link ($rpt_tag->term_id) . '" rel="tag">' . $rpt_tag->name . '</a></span>';
}
} else {
$recentTags .= 'There are no popular tags right now.';
}
$recentTags .= '</div><!--.recent-popular-tags-->';
return $recentTags;
}
function rpt_display_content ($content){
if(is_single()){
$rpt_display = wpse_158425_display_terms();
$content .= $rpt_display;
}
return $content;
}
add_filter('the_content', 'rpt_display_content');
?>
You can further simplify this by making wpse_158245_get_terms()
accept variables and passing the options in that manner, and setting default variables.
I did not have a chance to run this, you might run into some object/array errors, but you should be good to go.