Selecting data in a specific date range

Sometimes, you have to return all objects created in a specific date range, e.g. you want to return all posts created in the last thirty days. Thanks to the nice functions for date operations provided by MySQL this task is simple:

$this->Post->findAll(
    'WHERE Post.created >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)');

6 Comments

  1. Mandy
    Posted August 25, 2006 at 10:46 am | Permalink

    Thanks for the tip.

    Just one suggestion – the code in all your posts is always cut off. Not a big thing but it would be great to find a solution to it.

    Thanks,
    Mandy.

  2. majna
    Posted August 25, 2006 at 11:28 am | Permalink

    Please put your code in scroll-div.

  3. Posted August 25, 2006 at 3:34 pm | Permalink

    Good tip. Like Mandy said, though, the code being cut off can be a bit annoying. I do realize I can copy/paste it just fine, though.

    I think adding an overflow: auto to your #content pre element would cause it to scroll. Might want to also put a height on it so the code blocks are all a fixed height.

    Just some thoughts, thanks for your blog. I’m learning cake and liking it quite a bit so far.

  4. Posted August 25, 2006 at 4:02 pm | Permalink

    I just added a plugin that uses geshi to render inline source

    SyntHythol http://indyjt.com/software/ you might check it out, you just wrap the code in pre tags with a lang attribute of the language. See an example at http://blog.samdevore.com/cakephp-pages/old-fart-in-the-oven/ (you have to check the box, but I bet you have read the manual) ;)

    Sam D

  5. ricardo couto
    Posted August 25, 2006 at 5:28 pm | Permalink

    Is it right to put the “WHERE” in the argument?

  6. Posted August 26, 2006 at 8:54 am | Permalink

    @Mandy, majna, gRegor: Thanks for your feedback. I will switch to another theme today which should fix the issues with posted code snippets.

    @Sam D: The plug-in looks promising, but unfortunately I cannot install plug-ins here on wordpress :|

    @ricardo couto: The “WHERE” is not necessary, you can omit it, and it still works.


One Trackback/Pingback

  1. […] Selecting data in a specific date range « cakebaker Adding a where clause to $this->Post->findAll() (tags: cakephp sql) […]

%d bloggers like this: