MadDawgX Posted January 31, 2007 Share Posted January 31, 2007 Hey there. I need a way of determining whether or not the current date is BEFORE, AFTER, or ON the date that is represented by: $TaskDue = date("F j, Y",strtotime(mysql_result($rTask,0,'due'))); How would I do this? - Thanx. Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted January 31, 2007 Share Posted January 31, 2007 ok, the easiest way to compare dates is to use unix timestamp format. So you have two ways to do this... 1) Get the date from your database, use php to convert it to a Unix timestamp and then compare it to the current timestamp. 2) Use MySQL's timestamp function and just compare it in php. The first is longer, but the column type in MySQL doesn't matter too much The second is easier and involves changing your sql query very slightly, but is dependent on the column type in MySQL being either, date, or datetime. Which way do you think you'd like to go? Regards Huggie Quote Link to comment Share on other sites More sharing options...
MadDawgX Posted January 31, 2007 Author Share Posted January 31, 2007 Okay, well the column type is date, so would that mean the first option is probably best? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 31, 2007 Share Posted January 31, 2007 There may be an easier way, but... $databaseDate = strtotime(mysql_result($rTask,0,'due')); $taskDue = $databaseDate; if(date("Y-m-d") == date("Y-m-d", $taskDue)){ // it's today }else if(time() > $taskDue){ // it's after today }else{ // it's before today } (not tested) Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted January 31, 2007 Share Posted January 31, 2007 In that case it's really simple... I can see from your existing code you're column is called due, so my sql statement is only doing to select from that column, but you can put all of your columns in... <?php // Query database and have the due column returned as a unix_timestamp $sql = "SELECT unix_timestamp(due) AS due FROM table_name WHERE column_name = 'condition'"; $result = mysql_query($sql) or die ("Unable to execute $sql: " . mysql_error()); // Get our timestamp into a php variable for comparison $due_date = mysql_result($result,0,'due'); if (date("Y-m-d") == (date("Y-m-d", $due_date)){ echo "On"; } elseif (time() > $due_date){ echo "After"; } else { echo "Before"; } ?> Edit: You're to quick for me Jesi Regards Huggie Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 31, 2007 Share Posted January 31, 2007 *sticks tongue out at Huggie* I win Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted January 31, 2007 Share Posted January 31, 2007 *sticks tongue out at Huggie* I win I think your 'posts per day' needs a bit of touching up though and just you wait, I'll be a recommended Freak any day soon I'm sure... hehe Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.