Solved

Days between two dates

Posted on 2010-11-26
6
407 Views
Last Modified: 2012-05-10
I'm trying to compare two days that I pull out of a mysql query and get the difference between them

i.e. the number of days between 2008-01-27 and 2009-04-25.

Is there a simple way of doing this?
0
Comment
Question by:PeterErhard
  • 3
  • 3
6 Comments
 
LVL 37

Expert Comment

by:Harisha M G
ID: 34220721
0
 

Author Comment

by:PeterErhard
ID: 34220726
Thanks for that.

How come this doesn't work?
Error
SQL query: 

SELECT DateCreated, DATE( ) - TO_DAYS( DateCreated ) 
FROM TABLE
LIMIT 0 , 30

MySQL said: 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') - TO_DAYS( DateCreated )
FROM AskTheSpider
LIMIT 0, 30' at line 1

Open in new window

0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 34220731
Change DATE( ) to TO_DAYS(DATE( ))
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:PeterErhard
ID: 34220735
Getting this now. Any Ideas?
SQL query: 

SELECT DateCreated, TO_DAYS( DATE( ) ) - TO_DAYS( DateCreated ) 
FROM AskTheSpider
LIMIT 0 , 30

MySQL said: 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) - TO_DAYS(DateCreated) 
FROM AskTheSpider
LIMIT 0 , 30' at line 1

Open in new window

0
 
LVL 37

Accepted Solution

by:
Harisha M G earned 500 total points
ID: 34220752
Sorry.. it should be CURDATE()

SELECT DateCreated, TO_DAYS(CURDATE()) - TO_DAYS( DateCreated )
FROM TABLE
LIMIT 0 , 30;

0
 

Author Comment

by:PeterErhard
ID: 34220756
Thanks very much, that did the trick :)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

770 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