Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Help with using DateDIFF function in MySQL

Posted on 2009-05-06
7
917 Views
Last Modified: 2012-05-06
Hey all,

Thanks in advance for any help that you can render!  I'm currently extremely frustrated by a problem trying to use DateDiff in one of my SQL queries.

The code is shown below and i've got a commented out line that runs fine but when I try to add in a DateDiff check the function still runs but it doesn't actually return results based on that datediff check as far as I can tell.

NEXTREVIEWREMIND is a Date field that stores the next time I want to trigger the operations I do after this sql query.

For example, NEXTREVIEWREMIND might be 2009-05-13 (next week) and based on $curdate I know that 2009-05-13 - 2009-05-06 = 7 which is > 0 so it should not be a match.  In my database i've seeded some values that should match including putting a NEXTREVIEWREMIND date on an entry of 2009-05-06 which if we do the math should mean that DateDIFF == 0 which will be true.  The other conditions are also met (NUMTIMESREMINDED is 0 which is less than the max of 3 and the status is active).

Any ideas on what i'm doing wrong or even a good way to test out datediff in some sort of debug mode?

Thanks,
Nathan

$curdate = date("Y-m-d");
$sql2 = "SELECT * FROM $tableUsers WHERE STATUS = 'ACTIVE' AND CURRENTLYREVIEWING = 1 AND DATEDIFF(NEXTREVIEWREMIND, $curdate) <= 0 AND NUMTIMESREMINDED < $maxremind";
//$sql2 = "SELECT * FROM $tableUsers WHERE STATUS = 'ACTIVE' AND CURRENTLYREVIEWING = 1 AND NUMTIMESREMINDED < $maxremind";
$sql_result2 = mysql_query ($sql2, $connection ) or die ('request "Could not execute SQL query" '.$sql);

Open in new window

0
Comment
Question by:ironwill96
  • 3
  • 3
7 Comments
 
LVL 19

Expert Comment

by:bevhost
ID: 24321924
SELECT DATEDIFF(now(),NEXTREVEIWREMIND) from $tableUsers LIMIT 0,20
Should give you a list of what DATEDIFF is returning.
0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24322833
Hi,

You can check the return value by :

$datediff = date('Y-m-d');

$sql2 = "SELECT DATEDIFF('$datediff',NEXTREVEIWREMIND) FROM $tableUsers";
$run2 = mysql_query($sql2);
$row2 = mysql_fetch_row($run2);
echo $row2[0];
0
 
LVL 2

Author Comment

by:ironwill96
ID: 24324109
I ran a query from phpmyadmin doing a SELECT DATEDIFF(NEXTREVIEWREMIND, "2009-05-06") AS DateDiff and it showed me the values for all of my differences and they are correct.  I see values of 7 where expected and 0 where expected.

The question still remains, why doesn't it work when DATEDIFF function is used as part of my WHERE clause?

Thanks,
Nathan
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 14

Expert Comment

by:shobinsun
ID: 24324254
Hi,

I think the problem is behind the other conditions clauses. Please check that with removing the clauses on by one.

Its working for me.

Regards
0
 
LVL 14

Accepted Solution

by:
shobinsun earned 500 total points
ID: 24324264
Hi,

this code I am using:

$datediff = date('Y-m-d');

$sql2 = "SELECT * FROM tablename WHERE DATEDIFF('$datediff','2008-11-30')>=0";

It works fine.

So you should ensure that the other conditions are right.

0
 
LVL 2

Author Comment

by:ironwill96
ID: 24329532
Ok found the problem while looking into the other clauses like you suggested.

The issue was in this section of the code: DATEDIFF(NEXTREVIEWREMIND, $curdate)

It needed to be: DATEDIFF(NEXTREVIEWREMIND, \"$curdate\")

Apparently you have to have the " " marks around dates inside of DATEDIFF which I didn't realize.
0
 
LVL 2

Author Closing Comment

by:ironwill96
ID: 31578842
Thanks for pointing me in the right direction!
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question