parse.com-php-library
parse.com-php-library copied to clipboard
Can't query using date
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.
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,
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.
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.
Hello, i tried your code but it does not work. I returns all the data in my parse table without filtering the record