How to convert number to month name in PHP?
The month number can be converted into a month name by using PHP functions. There are two functions to convert the given month number into month name.
Method 1: Using mktime() function: The mktime() function is an inbuilt function in PHP which is used to return the Unix timestamp for a date. The timestamp returns a long integer containing the number of seconds between the Unix Epoch (January 1, 1970, 00:00:00 GMT) and the time specified. The hour, minute, second, month, day and year are sent as parameters to the mktime() function and it returns an integer Unix timestamp on success and False on error.
Syntax:
int mktime( $hour, $minute, $second, $month, $day, $year, $is_dst)
Example:
<?php // PHP program to convert number to month name // Declare month number and initialize it $month_num =12; // Use mktime() and date() function to // convert number to month name $month_name = date("F", mktime(0, 0, 0, $month_num, 10)); // Display month name echo $month_name."\n"; ?> |
December
Method 2: Using DateTime::createFromFormat() function: The DateTime::createFromFormat() function is an inbuilt function in php which is used to parses a time string according to a specified format. This function accepts three parameters and returns new DateTime in success or false on failure.
Syntax:
DateTime::createFromFormat( $format, $time, $timezone )
Example:
<?php // PHP program to convert number to month name // Declare month number and initialize it $monthNum = 4; // Create date object to store the DateTime format $dateObj = DateTime::createFromFormat('!m', $monthNum); // Store the month name to variable $monthName = $dateObj->format('F'); // Display output echo $monthName."\n"; ?> |
April
Recommended Posts:
- How to get the number of days in a specified month using JavaScript ?
- How to convert a float number to the whole number in JavaScript?
- How to convert a string into number in PHP?
- How to convert a pixel value to a number value using JavaScript ?
- Convert a negative number to positive in JavaScript
- Convert boolean result into number/integer in JavaScript
- p5.js | month() function
- How to get the day and month of a year using JavaScript ?
- HTML | DOM Input Month min Property
- HTML | DOM Input Month Object
- HTML | <input type="month">
- HTML | DOM Input Month max Property
- HTML | DOM Input Month name Property
- HTML | DOM Input Month value Property
- How to get the first and last date of current month using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



