How to get the last day of a month

If you know what it is that you need, you’re halfway there. If you want to find the last day of a month, there must be a class of the same name. Besides, this class must implement some kind of DateTime interface or extend the same kind of abstract class. This fact indicates that the last day of a month is a datetime. And here you go, there is a TheLastDayOfAMonth indeed. That’s how you can obtain a last day of some datetime’s month:

(new TheLastDayOfAMonth(
    new FromISO8601('2020-02-21T23:28:04+07:00')
))
    ->value(); // returns 2020-02-29T00:00:00+07:00

If you want to find the last day of the current month, simply pass a current datetime:

(new TheLastDayOfAMonth(
    new Now()
))
    ->value();

Besides, you might want to find the first day of a month. It’s carried out with TheFirstDayOfAMonth and is pretty much the same with the above:

(new TheFirstDayOfAMonth(
    new FromISO8601('2020-02-21T23:28:04+07:00')
))
    ->value(); // returns 2020-02-01T00:00:00+07:00

Getting the first day of the current month is as easy as

(new TheFirstDayOfAMonth(
    new Now()
))
    ->value();

There are similar cases covered when you need to find the beginning of something or the end of something. For example, you can find the beginning of a day and a start of a week. Besides, you can add your own shortcut class for that.