📌  相关文章
📜  获取一个月内创建的所有用户 laravel - PHP 代码示例

📅  最后修改于: 2022-03-11 14:53:44.631000             🧑  作者: Mango

代码示例1
$users = User::select('id', 'created_at')
->get()
->groupBy(function($date) {
    //return Carbon::parse($date->created_at)->format('Y'); // grouping by years
    return Carbon::parse($date->created_at)->format('m'); // grouping by months
});

$usermcount = [];
$userArr = [];

foreach ($users as $key => $value) {
    $usermcount[(int)$key] = count($value);
}

for($i = 1; $i <= 12; $i++){
    if(!empty($usermcount[$i])){
        $userArr[$i] = $usermcount[$i];    
    }else{
        $userArr[$i] = 0;    
    }
}