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
$month_num =12;
$month_name = date ( "F" , mktime (0, 0, 0, $month_num , 10));
echo $month_name . "\n" ;
?>
|
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
$monthNum = 4;
$dateObj = DateTime::createFromFormat( '!m' , $monthNum );
$monthName = $dateObj ->format( 'F' );
echo $monthName . "\n" ;
?>
|
Last Updated :
30 Apr, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...