andrewscofield / parse.com-php-library

246 stars 137 forks source link

Can't query using date #135

Open amiriskander opened 10 years ago

amiriskander commented 10 years ago

I wanted to query on rows created in a time range between two dates, but I'm having a problem querying on the createdAt field.

I wanted to know the right way to query using a date.

Thanks.

fincom commented 10 years ago

Hi,

Date fields are not string, it's an object.

For example, you can do query like this: $query->whereLessThanOrEqualTo('date_start', array("__type"=>"Date","iso"=>date('Y-m-d\Th:i:s')));

That's work, but perhaps need to create specific where for Date Object.

Hope this help,

amiriskander commented 10 years ago

Hi, Thanks for your reply,
I have found a commit for a contributor with a function "whereBetweenOrEqualTo" which queries rows in between two date objects.

public function whereBetweenOrEqualTo($key, $startValue, $endValue){ if(isset($key) && isset($startValue) && isset($endValue)){ $this->_query[$key] = array( '$gte' => $startValue, '$lte' => $endValue ); }
else{ $this->throwError('the $key, $startValue and $endValue parameters must be set when setting a "where" query method');
} }

But I still need to have to query on the createdAt field which is of type DateTime using just a date not a datetime.

fincom commented 10 years ago

public function whereBetweenOrEqualDates($key, $startDate, $endDate){ if(isset($key) && isset($startDate) && isset($endDate)){ $this->_query[$key] = array( '$gte' => $startDate, '$lte' => $endDate ); }

call him for created_at without time: $startDate = array("type"=>"Date","iso"=>date('Y-m-d\T0:0:0'); $endDate = array("type"=>"Date","iso"=>date('Y-m-d\T0:0:0'); $query->whereBetweenOrEqualDates("createdAt", $startDate, $endDate);

It's dirty but it should work.

codepolariz commented 8 years ago

Hello, i tried your code but it does not work. I returns all the data in my parse table without filtering the record