Selecting data in a specific date range

Published on and tagged with cakephp  mysql  sql  tip

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:

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

7 comments baked

  • Mandy

    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.


  • majna

    Please put your code in scroll-div.

  • gRegor

    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.

  • Sam D

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

    SyntHythol you might check it out, you just wrap the code in pre tags with a lang attribute of the language. See an example at (you have to check the box, but I bet you have read the manual) ;)

    Sam D

  • ricardo couto

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

  • cakebaker

    @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.

  • links for 2006-08-31 « Richard@Home

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

© daniel hofstetter. Licensed under a Creative Commons License