Get the Year/Month/Day from a datetime in php?

Get the Year/Month/Day from a datetime in php?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Extracting Components from PHP DateTime Objects

If you are migrating legacy code that relies on date('w', $timestamp), date('n', $timestamp), or similar string format codes to modern object-oriented approaches, switching to DateTime objects is the right move. However, accessing specific calendar components like year, month, or day requires a different syntax than the procedural date() function.

The DateTime class stores date and time information in structured properties rather than expecting you to parse a string format mask. To get the day of the month, you access the $day property directly. For the month, use $month, and for the year, $year. These properties return integers by default, which is usually what you need for calculations or validation.

Here is how you extract these values from a standard DateTime instance:

$now = new DateTime();

echo $now->format('Y-m-d'); // Outputs formatted string if needed
echo 'Day: ' . $now->day;   // Returns integer 1-31
echo 'Month: ' . $now->month; // Returns integer 1-12
echo 'Year: ' . $now->year;   // Returns integer year

While the format() method is powerful for converting objects back into strings (like Y-m-d), accessing internal properties like $day or $month is significantly more efficient if you only need to manipulate those specific numbers. The DateTime object encapsulates the logic of calendar arithmetic, ensuring that you don't have to manually handle leap years or varying month lengths unless you explicitly tell it to.

Formatting vs. Properties

A common pitfall for developers coming from the date() format string paradigm is trying to use format codes like d or m directly on the object without the format() method call. The format() method is a formatter; it takes the internal state of the object and renders a string representation based on a pattern.

// Correct: Returns a string "2023-10-27"
$string = $now->format('Y-m-d'); 

// Incorrect: Returns false or throws an error in strict contexts if used as a property accessor logic
$year = $now->Y; // This does not work. Y is a format character, not a property name.

If you need to perform arithmetic, such as adding a month to a date, relying on properties is still useful but requires care. Simply incrementing $month will break if the date crosses into the next year or if the day number exceeds the days available in the new month (e.g., moving from Jan 31st to Feb).

$nextMonth = clone $now;
$nextMonth->modify('+1 month'); // DateTime handles rollover automatically

// But this approach fails:
$now->month++; // Will result in 13 if moving into next year, and day arithmetic is lost.

Avoiding Timestamps

You mentioned preferring to avoid UNIX_TIMESTAMP() or converting everything to timestamps. This is a valid architectural choice, especially when dealing with human-readable dates or when the time zone context matters. The DateTime class handles time zones natively via the $timezone property and the setTimeZone() method. When you work purely with properties, your code remains agnostic of the underlying Unix epoch conversion until you explicitly choose to convert it.

If you are building a framework or a library where this pattern is common, consider creating a helper function that wraps these extractions to keep your main business logic clean:

function getMonthName(DateTime $date): string {
    return $date->format('F'); // Returns full month name like "October"
}

function getYear(DateTime $date): int {
    return $date->year;
}

For more advanced scenarios, such as extracting the day of the week (equivalent to date('w')), you can use the format('N') method, which returns an integer representing the ISO-8601 weekday (1 for Monday, 7 for Sunday). This avoids the ambiguity of the w format character in PHP, which represents Sunday as 0.

Understanding the distinction between formatting strings and accessing internal properties ensures your code remains readable and efficient. By leveraging the DateTime object's built-in methods like modify() and format(), you gain robust calendar handling without needing to reinvent logic that has been standardized since PHP 5.

Our network