Find A Month From A Given Integer With PHP

7th June 2010 - 1 minute read time

If you need to know the month from a given integer (from 1 to 12) then you can use the following snippet. This will return the string "Feb".

date("M", mktime(0, 0, 0, 2));

This can be encapsulated into a function call that will take a number between 1 and 12 and return the corresponding string for that month. This function includes some simple error checking to make sure that the number is valid before trying to work out the date.

function getMonth($month) 
  if (!is_numeric($month) && $month  12) {
    return false;
  return date("M", mktime(0, 0, 0, $month));

Add new comment

The content of this field is kept private and will not be shown publicly.