Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 立即注册每小时用户数_Php_Php Carbon_Laravel 5.6 - Fatal编程技术网

Php 立即注册每小时用户数

Php 立即注册每小时用户数,php,php-carbon,laravel-5.6,Php,Php Carbon,Laravel 5.6,当我将$a++添加到循环中时,我得到: [ "2018-06-02 01:00:00 - 2018-06-02 00:00:00", "2018-06-02 01:00:00 - 2018-06-02 00:00:00", "2018-06-02 01:00:00 - 2018-06-02 00:00:00", "2018-06-02 01:00:00 - 2018-06-02 00:00:00", "2018-06-02 01:00:00 -

当我将
$a++
添加到循环中时,我得到:

[
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
   ]
它将添加到最后一个
$today
值新的
$a
值,但当
$a
值始终为1时,就好像它永远不会在最后一个
$today
值上运行,而是先运行

我做错了什么?
谢谢

这应该可以得到结果-仅使用SQL:

[
     "2018-06-02 01:00:00 - 2018-06-02 00:00:00",
     "2018-06-02 02:00:00 - 2018-06-02 01:00:00",
     "2018-06-02 04:00:00 - 2018-06-02 03:00:00",
     "2018-06-02 07:00:00 - 2018-06-02 06:00:00",
     "2018-06-02 11:00:00 - 2018-06-02 10:00:00",
     "2018-06-02 16:00:00 - 2018-06-02 15:00:00",
     "2018-06-02 22:00:00 - 2018-06-02 21:00:00",
     "2018-06-03 05:00:00 - 2018-06-03 04:00:00",
     "2018-06-03 13:00:00 - 2018-06-03 12:00:00",
     "2018-06-03 22:00:00 - 2018-06-03 21:00:00",
     "2018-06-04 08:00:00 - 2018-06-04 07:00:00",
     "2018-06-04 19:00:00 - 2018-06-04 18:00:00",
     "2018-06-05 07:00:00 - 2018-06-05 06:00:00",
     "2018-06-05 20:00:00 - 2018-06-05 19:00:00",
     "2018-06-06 10:00:00 - 2018-06-06 09:00:00",
     "2018-06-07 01:00:00 - 2018-06-07 00:00:00",
     "2018-06-07 17:00:00 - 2018-06-07 16:00:00",
     "2018-06-08 10:00:00 - 2018-06-08 09:00:00",
     "2018-06-09 04:00:00 - 2018-06-09 03:00:00",
     "2018-06-09 23:00:00 - 2018-06-09 22:00:00",
     "2018-06-10 19:00:00 - 2018-06-10 18:00:00",
     "2018-06-11 16:00:00 - 2018-06-11 15:00:00",
     "2018-06-12 14:00:00 - 2018-06-12 13:00:00",
     "2018-06-13 13:00:00 - 2018-06-13 12:00:00",
   ]
将返回以下内容:

// Only select users with `created_at` greater than today - start of the day
User::where('created_at', '>', now()->startOfDay())
    // Select created_at hour and count for that hour
    ->selectRaw('date_format(created_at, \'%Y-%m-%d %H:00:00\') as hour, count(users.id) as count')
    // Group by created_at hour
    ->groupBy(\DB::raw('date_format(created_at, \'%Y-%m-%d %H:00:00\')'))
    ->get()
编辑

为了得到所有24小时,我会这样做:

[{hour: '2018-06-02 20:00:00', count: 5}, {hour: '2018-06-02 21:00:00', count: 3}, ...]
//使用1个查询获取所需用户
$users=User::where('created_at','>',now()->startOfDay())->get();
$result=[];
对于($hour=0;$hour<24;$hour++){
$result[]=[
“小时”=>$hour,
“计数”=>$users->filter(函数($user)使用($hour){
//查找小时等于当前迭代的用户
返回$user->created_at->hour==$hour;
})->count(),
];
}
返回$result;

所以您只需要小时数和在该小时内注册的用户数?是的,对于今天的每个小时,您可以像
选择小时(datetimecolumn),按小时(datetimecolumn)从表组中计数(*)
或者您可以对前13个小时执行
substr
DateTimeColumn的字符按小时从数据库中获取并没有问题,它的问题是添加一小时24次。我没有问题找到某人注册的日期和时间我需要所有时间,如24小时,如[{hour:'2018-06-02 00',count:0},{hour:'2018-06-02 01',count:1},{hour:'2018-06-02 02 02',count 0},]等等所以我可以把它作为数据添加到我的chartjs中。你试过运行这个查询吗?它按一小时分组,我只是在末尾添加了
00:00
,以使大多数时间分析器更能接受它。如果您希望它看起来像
2018-06-02 00
,只需从
'%Y-%m-%d%H:00:00
中删除
:00:00
,我这样做了,并且在用户注册的几个小时后才返回。啊,我现在明白了。是的,这不会得到24小时,只有那些有人注册的。我会给你安排一个solution@StupidKid在上一次迭代中有一些错误,现在应该可以了。测试
[{hour: '2018-06-02 20:00:00', count: 5}, {hour: '2018-06-02 21:00:00', count: 3}, ...]
// Fetch needed users with 1 query
$users = User::where('created_at', '>', now()->startOfDay())->get();

$result = [];
for ($hour = 0; $hour < 24; $hour++) {
    $result[] = [
        'hour' => $hour,
        'count' => $users->filter(function ($user) use ($hour) {
            // Find users where hour equals to current iteration
            return $user->created_at->hour == $hour;
        })->count(),
    ];
}

return $result;