gabrielp Posted January 14, 2008 Share Posted January 14, 2008 Hello there, I need to do the following calculations... I have a starting date, and I need to know the exact date from that day + x number of months. $start_date = '2008-01-11 06:38:44'; $qty_months = 13; $final_date = ?????; Thanks, Gabriel. Quote Link to comment https://forums.phpfreaks.com/topic/85948-solved-calculating-dates/ Share on other sites More sharing options...
dooper3 Posted January 14, 2008 Share Posted January 14, 2008 Do it like this: $start='2008-01-11 06:38:44' $months_to_add=13; $qty_years=floor($months_to_add/12); $qty_months=$months_to_add%12; $bits=explode("-",$start); $year=$bits[0] $month=$bits[1]; $newyear=$year+$qty_years; $newmonth=$month+$qty_months; $newdate=$newyear."-".$newmonth."-".$bits[2]; That's probably not the best way of doing it, but it should work. I'm assuming that your date format is YYYY-MM-DD, not YYYY-DD-MM. Quote Link to comment https://forums.phpfreaks.com/topic/85948-solved-calculating-dates/#findComment-438814 Share on other sites More sharing options...
gabrielp Posted January 14, 2008 Author Share Posted January 14, 2008 This works GREAT Thanks so much Gabriel. Quote Link to comment https://forums.phpfreaks.com/topic/85948-solved-calculating-dates/#findComment-438825 Share on other sites More sharing options...
kenrbnsn Posted January 14, 2008 Share Posted January 14, 2008 A much simpler way: <?php $start='2008-01-11 06:38:44'; $months_to_add=13; $new_date = date('Y-m-d',strtotime($start . ' +' . $months_to_add . ' months')); echo $new_date; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/85948-solved-calculating-dates/#findComment-438847 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.