relic1980 Posted December 11, 2007 Share Posted December 11, 2007 I'm trying to figure out how many months there are between today and a future date. I don't want to subtract time() from a futuredate as it will not be accurate for certain months. I can't multiply 60 seconds x 60 minutes x 24 hours x 30 days, as some months have 28, 29 and 31 days. For example from today to Feb 10, 2008 - the answer should be 3 months and not 89 days (which is less than 3 months) Any help would be greatly appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/81109-calulate-number-of-months-remaining/ Share on other sites More sharing options...
drakal30 Posted December 11, 2007 Share Posted December 11, 2007 I am not an expert but I can give you an example perhaps you can build upon it $curDate = mktime(0,0,0,date('m'), date('d'), date('Y')); $futureDate = mktime(0,0,0,month,day,year); $numberofdays = round(($furDate - $curDate)/86400); That will give you exactly the number of days until the event. Which you can then further deduce the number of months until that date. Quote Link to comment https://forums.phpfreaks.com/topic/81109-calulate-number-of-months-remaining/#findComment-411568 Share on other sites More sharing options...
kenrbnsn Posted December 11, 2007 Share Posted December 11, 2007 The following does, what I believe, is close to what you want. For testing, I generate a random date in the future. <?php $future = rand(1,12) . '/' . rand(1,28) . '/' . rand(2008,2010); // generate a random date in the future $fd = strtotime($future); $nw = time(); $cur_month = date('F'); $num_months = 0; for ($dt = $nw; $dt <= $fd; $dt += 86400) if (date('F',$dt) != $cur_month) { $num_months++; $cur_month = date('F',$dt); } echo 'Number of months between now and ' . $future . ' is ' . $num_months . "<br>\n"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/81109-calulate-number-of-months-remaining/#findComment-411571 Share on other sites More sharing options...
relic1980 Posted December 11, 2007 Author Share Posted December 11, 2007 Thanks for the response, but this is not 100% accurate. If I assign the future date as March 1, 2008, I get 2 months as the difference. Number of months between now and 03/01/2008 is 2 Quote Link to comment https://forums.phpfreaks.com/topic/81109-calulate-number-of-months-remaining/#findComment-411582 Share on other sites More sharing options...
kenrbnsn Posted December 11, 2007 Share Posted December 11, 2007 Try this instead: <?php $future = rand(1,12) . '/' . rand(1,28) . '/' . rand(2008,2010); $fd = strtotime($future); $nw = time(); $num_months = 0; while ($nw <= $fd) { $num_months++; $nw = strtotime('+1 month',$nw); } echo 'Number of months between now and ' . $future . ' is ' . $num_months . "<br>\n"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/81109-calulate-number-of-months-remaining/#findComment-411587 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.