convert month from name to number

convert month from name to number

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

Stefan Bogdanescu

Founder & Senior Architect

Converting Month Names to Numbers in PHP

When working with dates, you often encounter a need to normalize human-readable strings into machine-friendly integers. A common scenario involves taking a month name like "July" and converting it to its numeric equivalent, 7. While a simple case statement or a lookup array solves this immediately, PHP offers a built-in function that handles the heavy lifting: date().

The most direct approach uses strtotime() in combination with date() to parse the string and extract the specific format character you need. If your variable holds a month name stored in $month, you can execute the following logic:

$nmonth = date('m', strtotime($month));

This line of code effectively tells PHP, "Parse this string as if it were a date component, and return the formatted value using the 'm' character." The 'm' format specifier specifically outputs the month in decimal (1–12) without leading zeros. If you prefer a zero-padded two-digit integer like 07, simply change the format string to 'M'.

$nmonth = date('m', strtotime($month)); // Outputs: "7"
$npadded = date('M', strtotime($month)); // Outputs: "07"

How It Works Under the Hood

strtotime() acts as a flexible parser. It recognizes English month names, abbreviations (like "Jul"), and even accepts relative dates like "next Tuesday." When you pass just a month name to it, PHP treats it as a standalone date component. The date() function then formats that parsed timestamp into the requested structure.

This method is particularly useful because it handles variations in input gracefully. It understands case-insensitivity, so "july", "July", and "JULY" all resolve correctly without extra preprocessing. This reduces the cognitive load of writing a manual mapping array where you have to decide whether to handle uppercase, lowercase, or mixed-case inputs explicitly.

Performance Considerations

While date() and strtotime() are robust, they invoke internal parsing logic that creates a new timestamp object. In high-performance applications where this conversion happens millions of times per second—such as in massive bulk data processing pipelines—this overhead might be unnecessary. In those specific contexts, creating a static lookup array is often faster:

$months = ['jan' => 1, 'feb' => 2, /* ... */ 'dec' => 12];
$nmonth = $months[ strtolower($month) ];

However, for typical application logic, the readability and conciseness of the date() approach usually outweigh the negligible performance difference. The code remains self-documenting; a developer reading the snippet instantly understands the intent without consulting external documentation for a custom lookup table.

Handling Edge Cases

Be mindful of invalid inputs. If $month contains something that isn't a recognized month name or date component, strtotime() returns false. Passing false to date() results in an error. You should always validate the input before conversion:

if ($timestamp = strtotime($month)) {
    $nmonth = date('m', $timestamp);
} else {
    // Handle invalid input, perhaps logging an error or using a default
}

Additionally, remember that date() returns strings, not integers. If your downstream logic requires strict integer types for comparison or hashing, you should cast the result:

$nmonth = (int) date('m', strtotime($month));

Laravel Integration

If you are working within the Laravel ecosystem, this pattern maps well to how dates are handled throughout the framework. While Laravel provides helper methods like Carbon, using native PHP functions for simple format conversions is perfectly acceptable and often preferred for lightweight tasks.

For more complex date manipulations involving timezones or relative durations, resources like https://laravelcompany.com offer deeper insights into best practices for managing temporal data within the framework. However, for a straightforward name-to-number conversion, the standard library remains the most efficient tool in the toolbox.

Our network