php - 如何在 Laravel 5.3 中的查询生成器中使用时间戳来跟踪最近 5 天注册的用户

标签 php mysql laravel-5.3

我想列出最近 5 天内注册的用户。要列出所有用户 1,请使用以下查询。

$users = User::select('*')->orderby('created_at', 'desc')->get();

并像这样在 View 中获取它。

<table class="table table-hover">
                            <tbody><tr>
                                <th>ID</th>
                                <th>User Name</th>
                                <th>Email</th>
                                <th>Country</th>
                                <th>Date</th>
                                <th>Points</th>
                            </tr>

                            <?php foreach ( $users as $u ){ ?>

                            <tr>
                                <td>{{ $u->id }}</td>
                                <td>{{ $u->user_name }}</td>
                                <td>{{ $u->email }}</td>
                                <td>{{ $u->country }}</td>
                                <td>{{ $u->created_at }}</td>
                                <td>{{ \App\Points::getUserPoints($u->id) }}</td>
                            </tr>



                            <?php }?>

                            </tbody></table>

但是获取过去 5 天内注册的用户的查询是什么..?任何人请告诉我一个好的查询..

最佳答案

您可以使用Carbon ( docs ) 执行此操作:

    $now = Carbon::now();
    $users = User::where('created_at', '>=', $now->subDays(5))->get();

确保“use Carbon\Carbon;”位于文件顶部。

关于php - 如何在 Laravel 5.3 中的查询生成器中使用时间戳来跟踪最近 5 天注册的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42341981/

相关文章:

mysql - Xampp 升级后,我的 table 上出现错误 #1146

php - 从 laravel 查询到 wordpress 数据库

php - 添加到购物车功能错误[重复项]

php - 我怎样才能准确地看到 PHP 向客户端输出了什么?

php - 来自 mysql 服务器的 ajax.reload 出现错误 : property is undefined

php - 这段带有 PDO 的代码好吗(PHP)?

php - Laravel whereIn 查询中未找到列表值?

php - 如何通过此文件上传传递 csrf token

php - Telegram 通过 bot api 添加成员到 channel

php - 不使用 Composer 将 HTML 包添加到 Laravel