laqutus Posted October 6, 2008 Share Posted October 6, 2008 Im new to PHP and programming as a whole, I need some help with this problem with dates, I need to increment the date by 2 months so thats there is an exp[iry date 2 months from current date. However for some reason I can't get this to work and my code has turned into a right mess. It was working but now we are in the 10th month and it now sets the expiry date to 02 and not 12.........There must be a better way to do this?????? Please help....... Existing code: //Set date $add= date('Y/m/d'); // Break apart date $nadd = explode("/", $add); // Trim the integer if it is less than 10 if ($nadd[1] < 10){ $nads = ltrim($nadd[1], '0'); } $nadd[1] = $nads + $run; // Check if value after calculation is still below 10 if it is then add back the leading zero if ($nadd[1] < 10){ $nadd[1] = 0 . $nadd[1]; } // Set all the segments of the array $newad = array($nadd[0] , $nadd[1] , $nadd[2]); // Join the date string back together $dies = implode("/", $newad); I know this code is poor, but I dont know any better at present, any help would be useful. Thanks in advance Dan Quote Link to comment https://forums.phpfreaks.com/topic/127233-solved-newbie-problem-with-dates/ Share on other sites More sharing options...
CroNiX Posted October 6, 2008 Share Posted October 6, 2008 you can use mktime() which will create a unix timestamp and add 2 months to the months. <?php $newdate=mktime(0, 0, 0, date('m')+2, date('d'), date('Y')); echo date('Y/m/d', $newdate); results: 2008/10/06 Quote Link to comment https://forums.phpfreaks.com/topic/127233-solved-newbie-problem-with-dates/#findComment-658082 Share on other sites More sharing options...
F1Fan Posted October 6, 2008 Share Posted October 6, 2008 Use strtotime(). It is designed for this. <?php $olddate = date('Y-m-d'); // input date $newdate = date('Y-m-d',strtotime("$olddate +2 months")); // output date ?> Quote Link to comment https://forums.phpfreaks.com/topic/127233-solved-newbie-problem-with-dates/#findComment-658086 Share on other sites More sharing options...
laqutus Posted October 6, 2008 Author Share Posted October 6, 2008 you can use mktime() which will create a unix timestamp and add 2 months to the months. <?php $newdate=mktime(0, 0, 0, date('m')+2, date('d'), date('Y')); echo date('Y/m/d', $newdate); results: 2008/10/06 Thank you so much, that sure is efficient code, christ mine was terrible.... Thanks again!! :-) Quote Link to comment https://forums.phpfreaks.com/topic/127233-solved-newbie-problem-with-dates/#findComment-658111 Share on other sites More sharing options...
laqutus Posted October 6, 2008 Author Share Posted October 6, 2008 Use strtotime(). It is designed for this. <?php $olddate = date('Y-m-d'); // input date $newdate = date('Y-m-d',strtotime("$olddate +2 months")); // output date ?> Thanks also a very good version, works great.........Thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/127233-solved-newbie-problem-with-dates/#findComment-658112 Share on other sites More sharing options...
Barand Posted October 6, 2008 Share Posted October 6, 2008 Beware of adding months when the day is 31st of a month eg this month (Oct) <?php $t = mktime (0,0,0,date('m')+1, 31, 2008); echo date ('d M Y', $t); // 01 Dec 2008 ( ie 31st Nov ) ?> Quote Link to comment https://forums.phpfreaks.com/topic/127233-solved-newbie-problem-with-dates/#findComment-658278 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.