我有下面的查询,但它返回了一个空数组(我知道这个查询应该返回一条记录)
$created_at = date("Y-m");
$content = ContentModel::where('userId', $id->_id)
->where('created_at', 'like', "%{$created_at}%")
->orderBy('fav', 'DESC')
->get();
如果我删除->where('created_at', 'like', "%{$created_at}%")
,它会返回所有内容,但我想要今年和月份的内容,但当我放入该内容时,查询不起作用。
数据库中的日期为ISODate格式"created_at" : ISODate("2015-02-03T16:29:26.965Z")
我猜是因为ISODate格式。我怎样才能得到我需要的结果?
感谢
使用以下代码:
$created_at = new DateTime(date('F jS Y h:i:s A', strtotime('first day of ' . date('F Y'))));
$content = ContentModel::where('userId', $id->_id)
->where('created_at', '>', $created_at)
->orderBy('fav', 'DESC')
->get();
在此代码中,$created_at
变量表示当前月份的第一个。