parse.com-php-library icon indicating copy to clipboard operation
parse.com-php-library copied to clipboard

Can't query using date

Open amiriskander opened this issue 11 years ago • 4 comments

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.

amiriskander avatar Jan 18 '14 13:01 amiriskander

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,

fincom avatar Jan 24 '14 17:01 fincom

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.

amiriskander avatar Jan 24 '14 17:01 amiriskander

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.

fincom avatar Jan 24 '14 19:01 fincom

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

codepolariz avatar Oct 19 '15 14:10 codepolariz