An expression for DateTime :: modify () to change the day of the month

I have an object DateTime, and I need to move it to the next day of the Xmonth. For example, if Xequal to 15:

2011-02-03 → 2011-02-15 # Before 15, stay this month

2011-02-15 → 2011-02-15 # Today is 15, stay today

2011-02-20 → 2011-03-15 # Later than 15, go to the next month

I know that I can use the combination DateTime::format()and DateTime::setDate(), but is it possible to make it with one click on DateTime::modify()?

!It should also work under . PHP/5.2.14

Expressions containing "day 15" are not even parsed.

+3
source share
1 answer
$x = 15; // day 15 of the month
$d = $date->format('d');
$m = $date->format('m');
$y = $date->format('Y');

$date->setDate($y , $m , $x); // set the wanted day for the month

//if the wanted day was before the current day, add one month
if( $d > $x ){it next month one.
   $date->modify($date, '+1 month');
}
+10
source

Source: https://habr.com/ru/post/1789696/


All Articles