×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Eray Demirel
Added: Nov 18, 2016 1:44 PM
Views: 13
Adds a number of months to DateTime object
  1. function add_months( $months, DateTime $object ) {
  2.     $d=clone $object;
  3.     $next = new DateTime($object->format('d-m-Y H:i:s'));
  4.     $next->modify('last day of +'.$months.' month');
  5.  
  6.     if( $object->format('d') > $next->format('d') ) {
  7.         return $d->add($object->diff($next));
  8.     } else {
  9.         return $d->add(new DateInterval('P'.$months.'M'));
  10.     }
  11. }