[PHP] Get the last day of a month

Created on November 30, 2026

Use the t format character to get the number of days in the month of a DateTime value. This example uses October 2019 in the Asia/Tokyo time zone, so the last day is the 31st.

<?php

$dateTime = new DateTime(
  '2019-10-01 00:00:00',
  new DateTimeZone('Asia/Tokyo')
);
echo $dateTime->format('t');

Output

31
PHP