How to Get the First and Last Day of the Current Month in PHP

In PHP, date('Y-m-01') gives the first day of the current month and date('Y-m-t') gives its last day. The t format supplies the number of days in the reference month, including 28 or 29 for February. Choose the time zone that defines your current month before calculating either date.

Get this month’s first and last day with date()

In the date format characters used by date(), Y is the year, m is the two-digit month, and t is the month length. The digits 01 appear as written. Capture one timestamp so both calls refer to the same instant.

<?php
$timestamp = time();

echo date('Y-m-01', $timestamp), PHP_EOL;
echo date('Y-m-t', $timestamp), PHP_EOL;

Save each PHP example as a separate file and run it with a command such as php month-dates.php. The first line is YYYY-MM-01 and the second is the last date in that month.

date() uses PHP’s default time zone. Check the runtime setting with date_default_timezone_get(). Jobs or servers configured differently can produce different months near the boundary.

Choose the time zone for the reference month

When a site or report uses a specific time zone, pass a DateTimeZone to the DateTimeImmutable constructor. This example captures the current time once in Seoul and formats it twice.

<?php
$timezone = new DateTimeZone('Asia/Seoul');
$now = new DateTimeImmutable('now', $timezone);

echo $now->format('Y-m-01'), PHP_EOL;
echo $now->format('Y-m-t'), PHP_EOL;

For example, 2024-02-29 15:30 UTC is 2024-03-01 00:30 in Seoul. The same instant belongs to February in UTC and March in Seoul. Use the time zone identifier your application or reporting rules require.

Check leap years and month lengths

Use a fixed reference date in place of 'now' to reproduce the output. The sample covers February in a leap year and a common year, a 30-day April, and a 31-day December.

<?php
$timezone = new DateTimeZone('Asia/Seoul');
$dates = ['2024-02-15', '2025-02-15', '2025-04-15', '2025-12-31'];

foreach ($dates as $input) {
    $day = new DateTimeImmutable($input, $timezone);
    echo $day->format('Y-m-01'), ' ', $day->format('Y-m-t'), PHP_EOL;
}
Reference date First day Last day
2024-02-15 2024-02-01 2024-02-29
2025-02-15 2025-02-01 2025-02-28
2025-04-15 2025-04-01 2025-04-30
2025-12-31 2025-12-01 2025-12-31

The leap-year February ends on 2024-02-29, and the common-year example ends on 2025-02-28. The same format works with the other month lengths.

Build a time range for a monthly query

To query one month of timestamped records, include the start of the first day and exclude the start of the following month. Set the reference to day 1 at midnight, then add a month with modify(). Starting from day 1 avoids overflow from adding a month directly on days 29–31.

<?php
$local = new DateTimeZone('Asia/Seoul');
$reference = new DateTimeImmutable('2024-02-29 15:30:00', $local);

$monthStart = $reference->setDate(
    (int) $reference->format('Y'),
    (int) $reference->format('n'),
    1
)->setTime(0, 0);

$nextMonthStart = $monthStart->modify('+1 month')->setTime(0, 0);
$utc = new DateTimeZone('UTC');

echo $monthStart->setTimezone($utc)->format('Y-m-d H:i:sP'), PHP_EOL;
echo $nextMonthStart->setTimezone($utc)->format('Y-m-d H:i:sP'), PHP_EOL;

Converting the February 2024 Seoul interval to UTC with setTimezone() gives 2024-01-31 15:00:00+00:00 as the start and 2024-02-29 15:00:00+00:00 as the exclusive end. Replace the reference string with 'now' to compute the current month.

Convert both bounds to the time basis used for storage and comparison, then bind them to created_at >= :start AND created_at < :end. This includes fractional seconds on the last day. Calculate the display date and the query’s end bound for their respective uses. The example prints P to show the offset. If your database expects UTC values without an offset, convert to UTC and bind the result of format('Y-m-d H:i:s').