How to target a specific day of the current month using DateTime :: modify?

I need to set a DateTime object on a specific day of the current month. I obviously can do this by getting the current month and creating a new DateTime or checking the current day and creating a DateInterval object to change the DateTime, but I just want to give the text arguments of DateTime :: modify.

I am looking for something like:

$datetime->modify('10th day this month');

but it gives me an error for example

PHP Warning: DateTime :: modify (): Failed to parse the time string (10th day of this month) at position 0 (1): Unexpected character in php shell code on line 1

and I don't seem to be able to find the right sentence construct to make PHP look pretty.

+4
source share
3

X- , :

$datetime->modify('last day of previous month')->modify('+10 day');

:

$datetime->format('Y-m-10');
+7

DateTime:: modify(), : -

$date = new \DateTime();
$date->modify($date->format('Y-m-10'));

, , , , , .

+3

I found that this saves you a few microseconds, to use setDate()both format()to extract the current year and month to satisfy setDate 3 arguments (but more detailed)

$date = new /DateTime();
$date->setDate($date->format('Y'), $date->format('m'), 10);
+1
source

All Articles