Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Sure, here are some examples of how to create a DateTimeImmutable object on a monthly basis:

  1. Creating a DateTimeImmutable object for the current month:
$now = new DateTimeImmutable();
$month = $now->format('Y-m-d');
echo $month;
// Output: 2022-12-01
  1. Creating a DateTimeImmutable object for a specific month:
$date = new DateTimeImmutable('2023-01-01');
$month = $date->format('Y-m-d');
echo $month;
// Output: 2023-01-01
  1. Creating a DateTimeImmutable object for the last day of the current month:
$last_day_of_month = new DateTimeImmutable('last day of this month');
$month = $last_day_of_month->format('Y-m-d');
echo $month;
// Output: 2022-12-31
  1. Creating a DateTimeImmutable object for the first day of next month:
$first_day_of_next_month = new DateTimeImmutable('first day of next month');
$month = $first_day_of_next_month->format('Y-m-d');
echo $month;
// Output: 2023-01-01