3

I have the following query but it's returning an empty array (I know for a fact that this query should return one record)

$created_at = date("Y-m");

$content = ContentModel::where('userId', $id->_id)
->where('created_at', 'like', "%{$created_at}%")
->orderBy('fav', 'DESC')
->get();

If I remove the ->where('created_at', 'like', "%{$created_at}%") it returns everything fine but I want content that was of this year and month but the query doesn't work when I put that in.

The date in the database is ISODate format "created_at" : ISODate("2015-02-03T16:29:26.965Z")

I'm taking a guess it's because of the ISODate format. How do I get the result I need?

Thanks

4

1 回答 1

1

Use this code instead:

$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();

In this code $created_at variable indicates the 1st of current month.

于 2015-02-26T11:44:04.180 回答