How do I target a specific day of the current month with DateTime::modify?
datetime, php
Solution
It isn't possible to get the Xth day of a given month directly, but you can achieve this with a simple workaround:
$datetime->modify('last day of previous month')->modify('+10 day');
Another alternative would be:
$datetime->format('Y-m-10');
Problem
I need to be able to set a DateTime object to a certain day of the current month. I can obviously do this by getting the current month and creating a new DateTime, or checking the current day and creating a DateInterval object to modify the DateTime, but I want to just give plain text arguments to DateTime::modify. I'm looking for something like: ``` $datetime->modify('10th day this month'); ``` but this gives me an error like PHP Warning: DateTime::modify(): Failed to parse time string (10th day this month) at position 0 (1): Unexpected character in php shell code on line 1 and I don't seem able to find the correct construction of the sentence to make PHP play nicely.