Jump to content

comparing todays daye with a value in a DATE field


jeff5656

Recommended Posts

I have a date field called bundle_date.  I want to see if this is equal to today's date and if it is, echo "the bundle date is today", but why doesnt the following code do that?  It does not display anything.  I'm sure i'm not comparing the dates correctly?

 

<?php 
$current_date = date ("m/d/y"); 

$consultsq3 = "SELECT * FROM icu INNER JOIN bundle ON icu.id_incr = bundle.pt_id AND icu.id_incr = ' " . $row['id_incr'] . " '"; 

$result3 = mysql_query ($consultsq3) or die ("Invalid query: " . mysql_error ());
while ($row3 = mysql_fetch_assoc ($result3)) {
	if ($row3['bundle_date'] == $current_date) {
		echo "the bundle date is today";
	}
}
?>

When comparisons fail, echo out or otherwise examine what is being compared. What does the matching data in your database look like, exactly?

 

And you should be using a standard mysql DATE data type (YYYY-MM-DD), because it requires less storage, it allows for direct sorting, it allows for  greater-than/less-than comparisons, and it allows you use a couple dozen built in date/time functions.

 

Also, if you only want to retrieve rows having today's date, you should include that condition in your query.

It doesn't realize these are both the same date?

Computers don't have the ability to realize anything. Computers only do exactly what their programs tell them to do.

 

First of all the format of those two values are different so they will never compare. Secondly, if the format was the same, the two values are not the same (allowing for the possibility that value posted from the database 2009-04-16 is one that would not have been a match for Apr. 21st in 04/21/09.)

 

Your database is using the standard DATE format YYYY-MM-DD (I'll assume it is actually a DATE data type.) For a comparison to work, you need the format in $current_date to be the same (YYYY-MM-DD.) I'll assume you want to do the comparison in php since you did not indicate you wanted the query to return rows that matched the current date.

 

Use the following to get the current date in a YYYY-MM-DD format -

 

$current_date = date ("Y-m-d");

 

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.