PHP计算某项操作每周发生的次数

PHP计算某项操作每周发生的次数,php,arrays,algorithm,date,comparison,Php,Arrays,Algorithm,Date,Comparison,我有以下数组数据结构: $records = []; $records[] = ['id' => 1, 'date' => '2017-03-12', 'operation' => 'sent_email']; $records[] = ['id' => 1, 'date' => '2017-03-13', 'operation' => 'sent_email']; $records[] = ['id' => 1, 'date' => '2017

我有以下数组数据结构:

$records = [];

$records[] = ['id' => 1, 'date' => '2017-03-12', 'operation' => 'sent_email'];
$records[] = ['id' => 1, 'date' => '2017-03-13', 'operation' => 'sent_email'];
$records[] = ['id' => 1, 'date' => '2017-03-13', 'operation' => 'sent_email'];
$records[] = ['id' => 1, 'date' => '2017-03-14', 'operation' => 'forgot_password'];
$records[] = ['id' => 1, 'date' => '2017-03-14', 'operation' => 'sent_email'];
$records[] = ['id' => 2, 'date' => '2017-03-14', 'operation' => 'sent_email'];
$records[] = ['id' => 2, 'date' => '2017-03-14', 'operation' => 'forgot_password'];
$records[] = ['id' => 1, 'date' => '2017-03-27', 'operation' => 'sent_email'];
$records[] = ['id' => 1, 'date' => '2017-03-29', 'operation' => 'sent_email'];
在这个数组中,我存储由网站访问者完成的操作。显然,访客可以通过id号来识别

我想做的是计算每个访问者一周内(包括周一和周日)使用“发送电子邮件”操作的次数

例如:“发送电子邮件”操作的第一条记录显示此操作发生在“2017-03-12”(星期日),因此这意味着id=1的用户仅在该周进行一次此操作

对于id=1的用户,接下来的三次“发送电子邮件”操作在另一周内发生了三次。(2017-03-132017-03-132017-03-14这三个日期属于同一周)

我知道我可能需要循环查看每一条记录,并以某种方式检查这些日期是否属于同一周,但我感到困惑和困惑,我不理解完成这些记录所需的逻辑步骤。如果有人能给我一个解释或伪代码,我会非常感激,不管是什么可以帮助我解决这个问题,我真的很喜欢自己解决问题,但因为我被卡住了,我只需要有人来帮助我开始工作


提前感谢您提供的任何帮助。

DateTime对象和strtotime()函数都理解相对日期字符串,因此您可以完成“last周二”和“3天前”之类的简单操作

一些让您开始的伪代码:

// figure out when your monday is
monday = ...

// figure out when your sunday is
sunday = ...

// loop over all records
foreach $record:

    // skip records outside the date range you want
    if date < monday or date > sunday then skip this record;

    // skip records not of the type you want
    if operation != email then skip this record;

    // register a hit for this user
    increment counter for user 
//计算出星期一是什么时候
星期一=。。。
//弄清楚你的星期天是什么时候
星期日=。。。
//循环所有记录
foreach$记录:
//跳过所需日期范围之外的记录
如果日期<周一或日期>周日,则跳过此记录;
//跳过非所需类型的记录
如果操作失败!=然后通过电子邮件跳过此记录;
//为这个用户注册一个点击
用户的增量计数器

DateTime对象和strotime()函数都理解相对日期字符串,因此您可以完成“上周二”和“3天前”之类的简单操作

一些让您开始的伪代码:

// figure out when your monday is
monday = ...

// figure out when your sunday is
sunday = ...

// loop over all records
foreach $record:

    // skip records outside the date range you want
    if date < monday or date > sunday then skip this record;

    // skip records not of the type you want
    if operation != email then skip this record;

    // register a hit for this user
    increment counter for user 
//计算出星期一是什么时候
星期一=。。。
//弄清楚你的星期天是什么时候
星期日=。。。
//循环所有记录
foreach$记录:
//跳过所需日期范围之外的记录
如果日期<周一或日期>周日,则跳过此记录;
//跳过非所需类型的记录
如果操作失败!=然后通过电子邮件跳过此记录;
//为这个用户注册一个点击
用户的增量计数器