如何获取今天在yii2中创建的项目数?

时间:2018-04-29 06:13:55

标签: yii2

我的收藏中的示例数据: 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);
 }

如何使用查找选择日期?

3 个答案:

答案 0 :(得分:2)

您可以使用count()函数

您可以使用count()函数和新表达式(' 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

答案 1 :(得分:1)

如下所示更改您的位置条件并尝试

public function count_users () {
       $cnt = Users::find ()->where('DATE(created_at)=CURDATE()')->count();
       return $cnt;
}

yii-db-query#count()-detail

答案 2 :(得分:1)

根据上述建议或使用scalar()

,您可以使用override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let h = tableView.dequeueReusableHeaderFooterView( withIdentifier: self.headerID) as! MyHeaderView if h.content == nil { let v = UINib(nibName: "MyHeaderView", bundle: nil).instantiate (withOwner: nil, options: nil)[0] as! MyHeaderViewContent h.contentView.addSubview(v) v.translatesAutoresizingMaskIntoConstraints = false v.topAnchor.constraint(equalTo: h.contentView.topAnchor).isActive = true v.bottomAnchor.constraint(equalTo: h.contentView.bottomAnchor).isActive = true v.leadingAnchor.constraint(equalTo: h.contentView.leadingAnchor).isActive = true v.trailingAnchor.constraint(equalTo: h.contentView.trailingAnchor).isActive = true h.content = v // other initializations for all headers go here } h.content.lab.text = // whatever // other initializations for this header go here return h } 将其缩减为单行
  

scalar():返回第一行中第一列的值   查询结果。

count()