Jump to content

[SOLVED] Matching Today's Date With a "Deadline" Field in the Database


CloudSex13

Recommended Posts

Hey there -

 

I was wondering if it was possible to, for example, take today's date via PHP and store it as a variable, and then run a query to see if it matches a deadline date in a database?

 

It sounds like it'll work, but just making sure.

 

And if that does work, is there a way to also accept matches where today's date could match a deadline even if it's up to 7 days later in the database compared to the current date?

 

I suppose I'm asking if there's a certain function to check the date range between the deadline and today's date, or if there's a certain method I should approach for this.

 

Thank you if so.

 

Best -

 

:)

short answer is YES. your description is a little too vague to provide a specific answer though. by the sounds of it, you should have a column in your table (let's call it 'deadline') of the dataype DATE. Then, you can use that in your SQL query...for instance:

 

All the entries where the deadline has passed:

SELECT * FROM tablename WHERE deadline < NOW()

NOW() in MySQL refers to the current date/time

 

All the entries for a specific day:

SELECT * FROM tablename WHERE deadline = '2009-03-01'

 

All the entries where the deadline is this month:

SELECT * FROM tablename WHERE deadline BETWEEN '2009-03-01' AND '2009-03-31'

 

I see!

 

Thanks rhodesa and Boo-urns.

 

I'm just confused on, in example, the variable $nextweek. Would I use strtotime there?

 

$today = date("F j, Y");
$nextweek = 

mysql_query("SELECT * FROM Tasks WHERE Deadline BETWEEN '$today' AND '$nextweek'");

 

EDIT:

 

The variable $today would be unecessary technically, as you mentioned NOW() could be used. So it'd be:

 

$today = date("F j, Y");
$nextweek = 

mysql_query("SELECT * FROM Tasks WHERE Deadline BETWEEN NOW() AND '$nextweek'");

yes, you could use strtotime():

//the strtotime() part gives one week from today as a timestamp
//Then date() formats it into MySQL DATE format
$nextweek = date('Y-m-d',strtotime('+1 week'));
mysql_query("SELECT * FROM Tasks WHERE Deadline BETWEEN NOW() AND '$nextweek'");

you should be able to use MySQL functions too though (instead of strtotime/date)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.