Solved

Sort by expiry date

Posted on 2008-10-15
11
341 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Format Data Field - SQL 11 37
Stored Proc - Rewrite 42 61
Inserting results of While loop to sql server 5 20
What is this? I've never seen this... 2 9
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

820 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