如何获取今天在 yii2 中创建的项目数? [英] How to get count of items which are created today in yii2?

查看:32
本文介绍了如何获取今天在 yii2 中创建的项目数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的集合中的示例数据:created_at : 2018-04-29 05:25:28.000Z

Sample data in my collection : created_at : 2018-04-29 05:25:28.000Z

我正在使用 TimestampBehavior,

'timestamp' => [
            'class' => TimestampBehavior::className(),
            'attributes' => [
                ActiveRecord::EVENT_BEFORE_INSERT => 'created_at',
                ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
            ],
            'value' => function() { $now = new \DateTime('NOW'); return new \MongoDB\BSON\UTCDateTime(strtotime($now->format("Y-m-d H:i:s"))*1000); },
        ],

这是我的计数功能:

 public function count_users () {
       $cnt = Users::find ()->select (['_id', 'created_at'])->where (['created_at'=>date ('Y-m-d')])->all ();
       return count ($cnt);
 }

如何使用带日期的查找选择?

How to use find select with a date?

推荐答案

你可以使用 count() 函数

You could use the count() function

您可以使用 count() 函数和 new Expression('NOW()')

You could use the count() function and new Expression('NOW()')

public function count_users () {
       $cnt = Users::find ()->select (['_id', 'created_at'])
        ->where (['created_at' => new \yii\db\Expression('curdate()')]->count();
       retur $cnt;
 }

yii-db-query

yii-db-query#count()-detail

这篇关于如何获取今天在 yii2 中创建的项目数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆