kproctor Posted March 12, 2017 Share Posted March 12, 2017 Hi, I have been looking all day for code to add $x to $date.I have been able to find examples to get me part way. But I am not able to get the answer I need. Here is a snipped of code I've been working with. <?php echo "<br>"; //function remainingTerm($trmStartDate, $LNTerm){ date_default_timezone_set('America/New_York'); // date format yyyy-mm-dd //http://stackoverflow.com/questions/10448119/how-to-calculate-the-difference-of-datetime-field-and-now-in-php $LNTerm = 60; $trmStartDate = 2016-09-01; $startDate=date_create($trmStartDate); $matDate = strtotime('"+'.$LNTerm. 'months"', strtotime($trmStartDate)); //$matDate = date_create($matDate); //$maturityDate=date_create("2022-12-12"); $diff=date_diff($startDate,$matDate); //echo $diff->format('%R%a days'); echo "<br>"; echo $matDate->format('Y-m-d'); //return $daysToMaturity; //} ?> Quote Link to comment Share on other sites More sharing options...
benanamen Posted March 12, 2017 Share Posted March 12, 2017 (edited) Add 60 days to date <?php $date = new DateTime('2016-09-01'); $date->add(new DateInterval('P60D')); echo $date->format('Y-m-d'); ?> Edited March 12, 2017 by benanamen Quote Link to comment Share on other sites More sharing options...
kproctor Posted March 13, 2017 Author Share Posted March 13, 2017 Hi, thank you for the reply. how do I change the number of days to a variable. I've tried this. $date->add(new DateInterval("'P".$LNTerm."M'")); Quote Link to comment Share on other sites More sharing options...
kproctor Posted March 13, 2017 Author Share Posted March 13, 2017 To help clariy I am trying to add X days to some days in the past. (this date will be greater than today). I want to take this date and subtract it from today to determine how many days until that date. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 13, 2017 Share Posted March 13, 2017 $LNTerm = 60; $LNUnit = 'D'; $dt = new DateTime('2017-02-01'); $di = new DateInterval("P$LNTerm$LNUnit"); $dt->add($di); echo $dt->format('Y-m-d'); //--> 2017-04-02 $days_until = $dt->diff(new DateTime())->days; echo "<br>$days_until"; //--> 19 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.