Jump to content

Archived

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

tqla

[SOLVED] MySQL help - Date field greater than or equal to today

Recommended Posts

Hello. I have a date field in my mysql table.

 

The dates in that field are all formatted like this: 2009-01-01

 

I need to count the rows that meet the criteria in my query below PLUS with a date greater than or equal to today?

 

$sql = "SELECT * FROM table WHERE field1 = '1' AND field2 = '0' AND field3 = '1' AND field3 >= (HERE IS WHERE IN NEED TO SAY GREATER THAN OR EQUAL TO TODAY)";

$result = mysql_query($sql) or die (mysql_error());
$numrows = mysql_num_rows($result);

	if($numrows >= 1){

		//I'll put code here if there are one or more rows that meet criteria

		} 

 

Is this possible? How?  ???

Share this post


Link to post
Share on other sites

How about making a variable with the current time and date?

 

  $dt2 = date('Y-m-d H:i:s');
$sql = "SELECT * FROM table WHERE field1 = '1' AND field2 = '0' AND field3 = '1' AND field3 >= " . $dt2 . ")";

 

$result = mysql_query($sql) or die (mysql_error());

$numrows = mysql_num_rows($result);

 

      if($numrows >= 1){

       

        //I'll put code here if there are one or more rows that meet criteria

 

        }

Share this post


Link to post
Share on other sites

Thanks lokie538 but that doesn't work. I think I need to convert field3 and the current date into something else and then do a comparison. Reading up on it now but so far no luck.

Share this post


Link to post
Share on other sites

What is the datatype of field3?

 

 

If it's a unix timestamp:

 

SELECT * FROM blah WHERE field3 >= UNIX_TIMESTAMP();

 

 

If it's datetime or date:

 

SELECT * FROM blah WHERE field3 >= NOW();

Share this post


Link to post
Share on other sites

I understand we're after today, not now, so I would try this way actually

 

If it's a unix timestamp:

 

SELECT * FROM blah WHERE field3 >= UNIX_TIMESTAMP(DATE(NOW()));

 

 

If it's datetime or date:

 

SELECT * FROM blah WHERE field3 >= DATE(NOW());

 

 

 

 

Share this post


Link to post
Share on other sites

Yes! Thank you Mchl and Corbin!!!

 

This worked perfectly!

 

SELECT * FROM blah WHERE field3 >= DATE(NOW());

 

:) :) :)

Share this post


Link to post
Share on other sites

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