编程语言
首页 > 编程语言> > php-计算频率

php-计算频率

作者:互联网

我正在创建一个论坛,并希望计算每天新帖子的出现频率.因此,每个帖子都有时间戳:

$post_1 = 1526083200;
$post_2 = 1524083200;
$post_3 = 1523083200;
$post_4 = 1522083200;

我要进行什么计算才能找出每天提交帖子的频率.最终输出示例:

echo 'Every '. $frequency .' day(s)';

解决方法:

您也许可以尝试这样的事情:

$post_1 = 1526083200;
$post_2 = 1524083200;
$post_3 = 1523083200;
$post_4 = 1522083200;

// I add all the value in an array then sort the array to get the min and max value
$date_array = [$post_1, $post_2, $post_3, $post_4];
sort($date_array);

// Now I can select the min date and the max date
$min_date = $date_array[0];
$max_date = $date_array[count($date_array) - 1];

// I calculate the diff to get the number of day during this period
$datediff = $max_date - $min_date;

// I divide this value with the number or article post during this period
$frequency = $datediff / count($date_array);

// Now I transform this value in number of day
$frequency = round($frequency / (60 * 60 * 24));

以您的示例为例,您将得到:

>文章数:4
>最晚日期:2018-03-26
>最晚日期:2018-05-12
>期间天数:46
>频率:12

那些有价值的东西对我来说听起来很不错,每12天发表一篇文章.

是您要找的东西吗?

标签:calculation,php,algorithm
来源: https://codeday.me/bug/20191025/1926010.html