Solved

Sort by expiry date

Posted on 2008-10-15
11
338 Views
Last Modified: 2013-12-13
I have a table of different competitions. Each as an expiry date in the form:

DD/MM/YYYY

at the moment I am counting the total number of entries using this query:

      $queryTotal = "SELECT COUNT(id) FROM archive_comps;";
      
      $totalResult = mysql_query($queryTotal);
      confirm_query($totalResult);
            
      $total = mysql_fetch_array($totalResult);

However i want to be able to count the entries which are still active ie. have not expired.

How do I do this?

Thanks
0
Comment
Question by:DrZork101
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 17

Expert Comment

by:houssam_ballout
ID: 22718818
you must compare their date with the date of today.

0
 
LVL 6

Expert Comment

by:RemcovC
ID: 22718844
This should do the trick:

$queryTotal = "SELECT COUNT(id) FROM archive_comps WHERE CDate(expirydate)>CURRENT_TIMESTAMP;";
0
 

Author Comment

by:DrZork101
ID: 22718922
when i use that it says:

FUNCTION databaseName1.CDate does not exist

0
 
LVL 6

Assisted Solution

by:RemcovC
RemcovC earned 250 total points
ID: 22718940
oops sorry, it should be cast(expirydate as datetime)
it's sql not vb ^^
0
 
LVL 6

Expert Comment

by:RemcovC
ID: 22718945
(The cast is not needed if the expiredate is already a datetime field)
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:DrZork101
ID: 22718980
the expiry date is stored in the database as DD/MM/YYYY

now its says:

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 ') > CURRENT_TIMESTAMP' at line 1

0
 
LVL 1

Expert Comment

by:gtsupport
ID: 22718979
Maybe this will work

$queryTotal = "SELECT COUNT(id) FROM archive_comps WHERE expirydate> now();";<!-- richText -->
0
 

Author Comment

by:DrZork101
ID: 22719007
the query from gtsupport does run, however does no come up with the right results.

does closingDate have to be converted to a specific formatting?
0
 
LVL 6

Expert Comment

by:RemcovC
ID: 22719024
yep to datetime format

you can do that by convert or cast
CAST (closigndate as date)
0
 

Accepted Solution

by:
DrZork101 earned 0 total points
ID: 22719190
Thanks remcovC but that did not work I have solved the problem using:

$queryTotal = "SELECT COUNT(id) FROM archive_comps WHERE STR_TO_DATE(closingDate,'%d/%m/%Y') > CURDATE()";
0
 
LVL 1

Expert Comment

by:gtsupport
ID: 22719364
Great that it answered your problem. I did not think that you would save the expired date field as a string. You might want to change it to a timestamp format for better performance. That way you will not need to call the STR_TO_DATE function.  It will also allow you to  index your  column  to make it faster to retrieve data.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now