• Friday, May 25, 2012
  • Add minutes or hours to current time mysql


    Writing a SQL query is always interesting, and for me it’s never boring task. Recently I was developing a CRM for my company, In that I need to send mail to the users who have scheduled their task as a reminder so I taught of using cron job for the same.Well now I need to fetch the users to schedule the mail, here is the challenge; I have to set the cron job to run the file every 15 minutes, so I have to take out the users from current time to next 15 minutes.

    We can achieve this by PHP, but I want to do it via mysql itself. After quick googling, I found a function called DATE_ADD().


    Syntax:

    DATE_ADD(date_time,INTERVAL expr type)

    My Query:

    SELECT * FROM table_task WHERE field_remainder BETWEEN NOW(AND DATE_ADD(NOW(), INTERVAL 15 MINUTE)

    So adding 15 minutes to the current time becomes easier now by using the DATE_ADD() function. Likewise you can also add days to the date. Check here for more detailed information on DATE_ADD() function.

    Share this post

    Kathirason Asokan
    About the Author
    I'm , I love writing code in PHP, and spending time with friends. My hobbies are blogging, surfing internet, juggling etc.,.

    0 comments:

    Post a Comment

     
    Copyright (c) 2011 - 2021 techispeaks.com. All rights reserved the content is copyrighted to Kathirason Asokan
    Creative Commons License