I think this is the idea. I can understand it.
<?php
class Trends
{
public function trending($min_tag_count = 100, $num_tags = 5)
{
// Get the number of hours
$num_hours = $this->getNumHoursToStartTrend($min_tag_count);
$tags = $this->getTagsByCountAndTime($num_hours);
// Get the first $num_tags
return array_slice($tags, 0, $num_tags);
}
protected function getNumHoursToStartTrend($min_tag_count = 100)
{
$hours = [];
do
{
$hours += $this->getNumTagsByHour();
// Add a day to the start date.
}while (count($hours) < $min_tag_count);
return $num_hours;
}
protected function getNumTagsByHour(\DateTime $start_date = 'NOW()', $num_hours = 24)
{
/*
* Group the number of tags by hour:
*
* SELECT HOUR(tag_dtg) AS tag_hour, COUNT(*) as hour_count
* FROM post_tags
* WHERE tag_dtg >= $start_date - $num_hours HOURS
* GROUP BY HOUR(tag_dtg)
* ORDER BY tag_hour
*/
return $hours;
}
protected function getTagsByCountAndTime($hours_from_now)
{
/*
* Get all of the tags in this timeframe
*
* SELECT tag, COUNT(*) as tag_count
* FROM post_tags
* INNER JOIN tags USING(id_tag)
* WHERE tag_dtg >= NOW() - $hours_from_now HOURS
* GROUP BY id_tag
* ORDER BY tag_count
*/
return $tags;
}
}