acidglitter Posted December 16, 2007 Share Posted December 16, 2007 If I have a date in a mysql table that's a few days away, how can I show how many days there are left until the date saved in the table? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 16, 2007 Share Posted December 16, 2007 You sort of lost me on your second line. Little more explanation please. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 16, 2007 Share Posted December 16, 2007 How is the date stored, mysql or unix? He wants to find out how long from now till the date in his DB. Quote Link to comment Share on other sites More sharing options...
acidglitter Posted December 16, 2007 Author Share Posted December 16, 2007 okay sorry in the table i could have the date 2007-12-18 saved. what mysql code would show that that date is 2 days away? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted December 16, 2007 Share Posted December 16, 2007 edit: I tried it with the date you gave me. I never tried anything like this before, so i randomly made my own script, and seems like it worked <?php $now = time(); $date = mktime(0,0,0,12,18,2007); $now = explode("-",date("y-m-d",$now)); $then = explode("-",date("y-m-d",$date)); $year = $now[0] - $then[0]; $month = $now[1] - $then[1]; $days = $then [2] - $now[2]; echo "Years left: ".$year; echo "<br>"; echo "Months left: ".$month; echo "<br>"; echo "Days left: ".$days; ?> Quote Link to comment Share on other sites More sharing options...
corbin Posted December 16, 2007 Share Posted December 16, 2007 You could also do it in SQL.... mysql> SELECT DATEDIFF('2007-12-25', CURDATE()); +-----------------------------------+ | DATEDIFF('2007-12-25', CURDATE()) | +-----------------------------------+ | 9 | +-----------------------------------+ 1 row in set (0.00 sec) Meaning, you could probably do something like: SELECT DATEDIFF(DATE(in_the_future_column), CURDATE()) as days_until Edit: Manual link: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_curdate Quote Link to comment Share on other sites More sharing options...
acidglitter Posted December 18, 2007 Author Share Posted December 18, 2007 thanks a lot for your help! i used SELECT DATEDIFF(DATE(in_the_future_column), CURDATE()) as days_until and its working perfectly 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.