jeff5656 Posted April 21, 2009 Share Posted April 21, 2009 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"; } } ?> Link to comment https://forums.phpfreaks.com/topic/154969-comparing-todays-daye-with-a-value-in-a-date-field/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 21, 2009 Share Posted April 21, 2009 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. Link to comment https://forums.phpfreaks.com/topic/154969-comparing-todays-daye-with-a-value-in-a-date-field/#findComment-815139 Share on other sites More sharing options...
jeff5656 Posted April 21, 2009 Author Share Posted April 21, 2009 When I echo row3[ventbundle] I get 2009-04-16 and when i echo current_date I get 04/21/09. So is this why the comparison failed? It doesn't realize these are both the same date? How do I do it so the comparison works? Link to comment https://forums.phpfreaks.com/topic/154969-comparing-todays-daye-with-a-value-in-a-date-field/#findComment-815176 Share on other sites More sharing options...
PFMaBiSmAd Posted April 21, 2009 Share Posted April 21, 2009 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"); Link to comment https://forums.phpfreaks.com/topic/154969-comparing-todays-daye-with-a-value-in-a-date-field/#findComment-815187 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.