Jump to content

Recommended Posts

Hey

 

Im just writing an additon to my invoicing system that returns all the piad invoices for the last tax year. I have a "paid" colum for my invoices which has data like, 01/01/2010 etc.

 

my sql query is like this

 

SELECT * FROM invoices WHERE paid LIKE '%/04/2009' AND paid LIKE '%/05/2009' AND paid LIKE '%/06/2009' ........ AND paid LIKE '%/01/2010' AND paid LIKE '%/02/2010'

 

etc, basically covering everything from 04/2009 to 03/2010 .. but iv got 12 AND LIKE bits in my querry which seems abit overkill, any easier/faster way?

 

Cheers!

Link to comment
https://forums.phpfreaks.com/topic/200771-quicker-way-to-do-this/
Share on other sites

If you are serious about making a real application (something that will be used outside of a programming classroom assignment), your first step will be to store your dates as a DATE data type.

 

As you have discovered by attempting to write the query in your first post in this thread, NOT using the data type that was designed for a specific purpose results in writing more difficult code and queries that take longer to execute.

 

Using a DATE data type will result in less storage requirements, faster queries, and simpler code.

 

You can use the mysql STR_TO_DATE() function directly in a query to INSERT any starting date format into a DATE data type (you can also use the STR_TO_DATE() function to populate a new DATE column from your existing data.) Or you can produce a DATE data type in your php code at the same time you are validating the supplied data values.

 

You can use the msyql DATE_FORMAT() function directly in a query to format a DATA data type as any format you want.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.