By ykyuen on
I have a content type with a date field and i want to select the nodes with a date field condition.
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'article')
->propertyCondition('status', 1)
->fieldCondition('field_article_date', 'value', '2013-04-13 00:00:00', '=');
$result = $query->execute();
The above query can retrieve articles withe field_article_date = 2013-04-13 00:00:00. but i couldn't figure out how to retrieve articles which have field_article_date < 2013-04-13 00:00:00. i tried
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'article')
->propertyCondition('status', 1)
->fieldCondition('field_article_date', 'value', '2013-04-13 00:00:00', '<=');
$result = $query->execute();
but it does not work, seems the field_article_date value is stored as string and cannot be compared by the <= or other similar operators.
Anyone ideas?
Thanks.
Comments
When creating a date field
When creating a date field you have three choices, which did you pick?
thx for your quick reply.
thx for your quick reply. =D
there are 3 options
1) Date
2) Date (ISO format)
3) Date (Unix timestamp)
i picked the 1st one.
so do u mean i should pick Date (Unix timestamp)?
For the logic you have you
For the logic you have you would need to use a unix timestamp and the correct mysql function to change the date string to a unix timestamp.
I would use views to make the list making it easier.
You could also look at how the date module handles this.
Replace core functions?
Hello Nevets,
So are you saying the core "entity" functions should be replaced with the contributed module "views"?
Sorry, I don't understand. Doesn't "views" use core functions? Not the other way around?
Are you saying that "views" is faster or more accurate than the core entity functions?
I am not a full time Drupal developer like you. Therefore, I am very curious about what one of this forum's moderator's opinion is :) Why use "views" instead of EntityFieldQuery. The original poster's sample is pretty simple. How would "views" be better?
Thank you for your thoughts.
"Thinking is the best way to travel."
I am saying if you want a
I am saying if you want a list of content filtered by date I would just use views
Why is "views" better than "EntityFieldQuery"?
Hello Nevets,
Thank you for your thoughtful reply:)
Again, I am not a full time Drupal developer like you. You are a forum moderator, therefore you opinion has weight :)
Can you detail why "views' is better than the core functions? Does views perform faster? Is "views" more accurate. Can't I simply take the results from a EntityFieldQuery, load and render instances, pass A result array to theme('table' ...)?
How is "views" better? Since this is the "coding forum". Specifically, for coding, how is "views" better. Is "views" easier to code with?
"Thinking is the best way to travel"
Sorry, i made a stupid
Sorry, i made a stupid mistake. actually the 2nd code snippet works too.
it throws an exception because i dun have any node satisfying the condition.
This should work for both Date and Date (Unix timestamp).
Sorry again and thanks nevets.