Reputation: 146390
I have a DateTime
object and I need to move it to the next day X
of month. E.g., if X
is 15:
2011-02-03 ⇒ 2011-02-15 # Earlier than 15, stay on this month
2011-02-15 ⇒ 2011-02-15 # Today's 15, stay on today
2011-02-20 ⇒ 2011-03-15 # Later than 15, move to next month
I know I can use a combination of DateTime::format()
and DateTime::setDate()
, but is it possible to make it with a single call to DateTime::modify()
?
! It must also work under PHP/5.2.14
.
Expressions that contain "day 15" do not even parse.
Upvotes: 3
Views: 10196
Reputation: 5303
$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 ){ // is next month's one.
$date->modify($date, '+1 month');
}
Upvotes: 10