calculating DATE differences

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tdsmithj
    New Member
    • Feb 2008
    • 1

    calculating DATE differences

    I am doing a registry system where you have to register and pay within 2 months before action is taken. I am trying to create an SQL query to sort out registered users who have yet to pay in a specific time.

    i come to a solution where you have to...

    select * from members where PaidStatus = 'new' && CURDATE() > DateReg + 2 months;

    but iam unsure how to calculate the 2months, i thought maybe a uni time which is around 5000000 but does not work. any suggestions.
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    Hi tdsmithj,

    Welcome to TSDN!!!

    You can calculate 2 months duration from registration date with the help of MySQL's built in function DATE_ADD().

    The following query will fetch all those members who have not paid within 2 months
    [CODE=mysql]select * from members where PaidStatus = 'new' AND CURDATE() > DATE_ADD(DateRe g, INTERVAL 2 MONTH);[/CODE]
    You can rewrite the query in this way too (this query is more efficient)
    [CODE=mysql]select * from members where PaidStatus = 'new' AND DATE_SUB(CURDAT E(), INTERVAL 2 MONTH) > DateReg;[/CODE]

    Comment

    Working...