How to show a number as a month?

I am trying to figure out how to show a number between 1 to 12, as the corresponding month.

Something similar to this:

$newmonth = 4;
echo "Month: " . date("M",$newmonth);

I want the output to show “Apr”.

Since I’m only working with a single date element (the month) I don’t have the ability to use the strtotime functon.

Any ideas how I could do this without having to build a reference table for the 12 months and corresponding days?

So you just want to display the number AND the three letter month abbreviation?
If yes, then concatenate the $newmonth variable separately:

$newmonth = 4;
echo "Month: " . $newmonth . ' ' . date("M");
$newmonth = 4;
echo DateTime::createFromFormat('!m', $newmonth)->format('M');
// Apr

Sure you can. Since you do not care about anything else than the month you can simply hard code in the rest.

$newmonth = 4;
echo date('M', strtotime($newmonth.'/01/2000'));
// Apr

I suggest using DateTime though as that’s pretty much the standard way of handling dates today.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service