Skip to content
Advertisement

The first day of the current month in php using date_modify as DateTime object

I can get the Monday of this week with:

JavaScript

I would like to get with the same ease the 1st of this month. How can I achieve that?

Advertisement

Answer

Requires PHP 5.3 to work (“first day of” is introduced in PHP 5.3). Otherwise the example above is the only way to do it:

JavaScript

In PHP 5.4+ you can do this:

JavaScript

If you prefer a concise way to do this, and already have the year and month in numerical values, you can use date():

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement