Skip to content
This repository has been archived by the owner on Feb 25, 2018. It is now read-only.

Can't query using date #135

Open
amiriskander opened this issue Jan 18, 2014 · 4 comments
Open

Can't query using date #135

amiriskander opened this issue Jan 18, 2014 · 4 comments

Comments

@amiriskander
Copy link

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
Copy link

fincom commented Jan 24, 2014

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
Copy link
Author

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
Copy link

fincom commented Jan 24, 2014

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
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants