【问题标题】:Calculating frequency计算频率
【发布时间】:2018-06-28 14:12:10
【问题描述】:

我正在创建一个论坛,并想计算每天新帖子的频率。所以,每个帖子都有时间戳:

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

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

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

【问题讨论】:

  • 你如何获得这些价值?
  • @MickaelLeger 它们存储在数据库中。
  • 您可以尝试选择按天获取总和或文章,然后用(number of post) / (number of day selected) no 计算频率?

标签: php algorithm calculation


【解决方案1】:

你可以试试这样的:

$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 天一篇文章的价值听起来不错。

这是你要找的吗?

【讨论】:

  • 解决方案是您发布的评论:(number of post) / (number of day selected)
  • 那么在您的示例中,您想要的频率是 4 / 46 = 0.0869 ?所以每 12 天以每天 0.0869 的频率发布一个帖子?
【解决方案2】:

假设您想要一般频率:

  • 频率 = 1 / 周期
  • 周期 = 两个帖子之间的平均时间 = 最早和最新帖子之间的时间 / 帖子数量 - 1
  • 最早和最新帖子之间的时间 = 最新帖子 - 最旧帖子

在你的例子中:

$post_1 = 1526083200;
$post_2 = 1524083200;
$post_3 = 1523083200;
$post_4 = 1522083200;
  • 最早和最新帖子之间的时间 = 1526083200 - 1522083200 = 4000000 秒 = 46,2962963 天
  • 周期 = 46,2962963/3 = 15.4320987667 天(两个帖子之间平均间隔 15 天)
  • 频率 = 1 / 15.4320987667 = 0.06479999999(平均每 0.0648 天发布一篇帖子)

【讨论】:

  • 您能否使用上面的时间戳变量在代码中演示这一点?目标是每天的频率。
  • @HenrikPetterson 也许我误解了你想要什么,希望通过示例更清楚
猜你喜欢
  • 1970-01-01
  • 2012-10-24
  • 2021-03-09
  • 2014-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-18
  • 2015-04-04
相关资源
最近更新 更多