Results from the most recent live auction are here .
21 members in the live chat room. Join Chat !
01-16-2007, 10:24 AM
· #1 NamePros Regular
Comparing MySQL data to Current Date
Hey All,
I have a database which stores the Date in 3 different fields. send_D for the day, in the format 01, send_M for the month, in the format 01, 02, 03 etc. again, and send_Y for the year, in the form 1992, 1995, 2007 etc.
What I want to know is how would I compare those 3 values to the current date, like how to do "if sql_date = CurrentDate, yada yada yada" in PHP.
Thanks,
Josh.
01-16-2007, 10:43 AM
· #2 Stud Sausage
Location: England
Join Date: Dec 2006
Posts: 1,545
NP$: 32.41 (
Donate )
There are a couple of ways to do this, the first is a little rough by using the date function:
PHP Code:
$str = "{$row['send_D']} {$row['send_M']} {$row['send_Y']}" ;
if( date ( 'd m Y' ) == $str )
{
// it is todays date.
}
A little more complicated but more useful method is to create a timestamp using strtotime.
Unfortunately strtotime will not accept the month in numeric terms so we will have to put it into text first
Example:
PHP Code:
$months = array( "January" , "February" , "March" , "April" , "May" , "June" ,
"July" , "August" , "September" , "October" , "November" , "December" );
$newmonth = $months [ $row [ 'send_M' ]];
$timestamp = strtotime ( $row [ 'send_D' ] . $newmonth . $row [ 'send_Y' ]);
if( date ( 'd m Y' ) == date ( 'd m y' , $timestamp ) )
{
// Itis todays date.
}
The good thing about that is you now have the timestamp for later use.
Personally i would recommend ditching your current database structure and using 1 field to store a timestamp or at he least NOW(). (The first being preferable).
Things to read: http://uk2.php.net/strtotime http://uk2.php.net/time http://uk2.php.net/date http://www.google.com/search?hl=en&...amp&btnG=Search http://dev.mysql.com/doc/refman/5.0...-functions.html
01-16-2007, 12:37 PM
· #3 Law and disorder
Name: Kate
Location: Expat
Join Date: Aug 2005
Posts: 5,481
NP$: 1585.11 (
Donate )
IMO it would be better to store dates in a DATE field.
In the meantime this should do the trick:
Quote:
SELECT *
FROM your_table
where TO_DAYS(curdate() )=TO_DAYS( concat(send_Y,'-',send_M,'-',send_D) )
01-16-2007, 12:39 PM
· #4 Stud Sausage
Location: England
Join Date: Dec 2006
Posts: 1,545
NP$: 32.41 (
Donate )
Originally Posted by sdsinc
IMO it would be better to store dates in a DATE field.
In the meantime this should do the trick:
And now we see the full reason why i should go off and learn more SQL syntax
My way is messy compared to this. Then again, still stands for:
Quote: like how to do "if sql_date = CurrentDate, yada yada yada" in PHP.
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off