Advertisement
Guest User

Untitled

a guest
Sep 18th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. <?php
  2. $date = new DateTime('2000-12-31');
  3.  
  4. $date->modify('+1 month');
  5. echo $date->format('Y-m-d') . "n";
  6.  
  7. $date->modify('+1 month');
  8. echo $date->format('Y-m-d') . "n";
  9. ?>
  10.  
  11. The above example will output:
  12. 2001-01-31
  13. 2001-03-03
  14.  
  15. <?php
  16. $d = new DateTime( '2010-01-31' );
  17. $d->modify( 'first day of next month' );
  18. echo $d->format( 'F' ), "n";
  19. ?>
  20.  
  21. echo Date("Y-m-d", strtotime("2013-01-01 +1 Month -1 Day"));
  22. // 2013-01-31
  23.  
  24. echo Date("Y-m-d", strtotime("2013-02-01 +1 Month -1 Day"));
  25. // 2013-02-28
  26.  
  27. echo Date("Y-m-d", strtotime("2013-03-01 +1 Month -1 Day"));
  28. // 2013-03-31
  29.  
  30. echo Date("Y-m-d", strtotime("2013-04-01 +1 Month -1 Day"));
  31. // 2013-04-30
  32.  
  33. echo Date("Y-m-d", strtotime("2013-05-01 +1 Month -1 Day"));
  34. // 2013-05-31
  35.  
  36. echo Date("Y-m-d", strtotime("2013-06-01 +1 Month -1 Day"));
  37. // 2013-06-30
  38.  
  39. echo Date("Y-m-d", strtotime("2013-07-01 +1 Month -1 Day"));
  40. // 2013-07-31
  41.  
  42. echo Date("Y-m-d", strtotime("2013-08-01 +1 Month -1 Day"));
  43. // 2013-08-31
  44.  
  45. echo Date("Y-m-d", strtotime("2013-09-01 +1 Month -1 Day"));
  46. // 2013-09-30
  47.  
  48. echo Date("Y-m-d", strtotime("2013-10-01 +1 Month -1 Day"));
  49. // 2013-10-31
  50.  
  51. echo Date("Y-m-d", strtotime("2013-11-01 +1 Month -1 Day"));
  52. // 2013-11-30
  53.  
  54. echo Date("Y-m-d", strtotime("2013-12-01 +1 Month -1 Day"));
  55. // 2013-12-31
  56.  
  57. $time = new DateTime('2014-01-31');
  58. echo $time->format('d-m-Y H:i') . '<br/>';
  59.  
  60. $time->add( add_months(1, $time));
  61.  
  62. echo $time->format('d-m-Y H:i') . '<br/>';
  63.  
  64.  
  65.  
  66. function add_months( $months, DateTime $object ) {
  67. $next = new DateTime($object->format('d-m-Y H:i:s'));
  68. $next->modify('last day of +'.$months.' month');
  69.  
  70. if( $object->format('d') > $next->format('d') ) {
  71. return $object->diff($next);
  72. } else {
  73. return new DateInterval('P'.$months.'M');
  74. }
  75. }
  76.  
  77. $start_dt = $starting_calculated;
  78.  
  79. $next_month = date("m",strtotime("+1 month",strtotime($start_dt)));
  80. $next_month_year = date("Y",strtotime("+1 month",strtotime($start_dt)));
  81.  
  82. $date_of_month = date("d",$starting_calculated);
  83.  
  84. if($date_of_month>28){
  85. $check_date = false;
  86. while(!$check_date){
  87. $check_date = checkdate($next_month,$date_of_month,$next_month_year);
  88. $date_of_month--;
  89. }
  90. $date_of_month++;
  91. $next_d = $date_of_month;
  92. }else{
  93. $next_d = "d";
  94. }
  95. $end_dt = date("Y-m-$next_d 23:59:59",strtotime("+1 month"));
  96.  
  97. $date = date('Y-m-d', strtotime("+1 month"));
  98. echo $date;
  99.  
  100. foreach(range(0,5) as $count) {
  101. $new_date = clone $date;
  102. $new_date->modify("+$count month");
  103. $expected_month = $count + 1;
  104. $actual_month = $new_date->format("m");
  105. if($expected_month != $actual_month) {
  106. $new_date = clone $date;
  107. $new_date->modify("+". ($count - 1) . " month");
  108. $new_date->modify("+4 weeks");
  109. }
  110.  
  111. echo "* " . nl2br($new_date->format("Y-m-d") . PHP_EOL);
  112. }
  113.  
  114. $startDate = new DateTime( '2015-08-30' );
  115. $endDate = clone $startDate;
  116.  
  117. $billing_count = '6';
  118. $billing_unit = 'm';
  119.  
  120. $endDate->add( new DateInterval( 'P' . $billing_count . strtoupper( $billing_unit ) ) );
  121.  
  122. if ( intval( $endDate->format( 'n' ) ) > ( intval( $startDate->format( 'n' ) ) + intval( $billing_count ) ) % 12 )
  123. {
  124. if ( intval( $startDate->format( 'n' ) ) + intval( $billing_count ) != 12 )
  125. {
  126. $endDate->modify( 'last day of -1 month' );
  127. }
  128. }
  129.  
  130. $datetime = new DateTime("2014-01-31");
  131. $month = $datetime->format('n'); //without zeroes
  132. $day = $datetime->format('j'); //without zeroes
  133.  
  134. if($day == 31){
  135. $datetime->modify('last day of next month');
  136. }else if($day == 29 || $day == 30){
  137. if($month == 1){
  138. $datetime->modify('last day of next month');
  139. }else{
  140. $datetime->modify('+1 month');
  141. }
  142. }else{
  143. $datetime->modify('+1 month');
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement