How to transform a month number to the relevant month name using PHP

Written by Ian Carnaghan · 23 sec read >

This can be obtained by combining the date function with the mktime function

For example if I want to get Jan from the number 1 which represents the first monthin the year I would use something similar to the below

$i = 1; //The number 1 represents January
echo date("M",mktime(0,0,0,$i,1,2010)); //This will output Jan

So if you want to output Feb the value of $i would be 2 etc


date = string date ( string $format [, int $timestamp ] ) (PHP 4 & 5)

mktime = int mktime ([ int $hour = date(“H”) [, int $minute = date(“i”) [, int $second = date(“s”) [, int $month = date(“n”) [, int $day = date(“j”) [, int $year = date(“Y”) [, int $is_dst = -1 ]]]]]]] )  (PHP 4 & 5)

Last Updated On March 22, 2018
Written by Ian Carnaghan
I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment. In my spare time I teach undergraduate classes in web development. Profile

Leave a Reply

Notify of