hi i am using PHP i want to pass an integer value (1-12) and get corresponding month name , is there a way in PHP to do that or i have to do t my own by initialize a month names array.
i want to do like
$month_name = get_month_name(1);
echo $month_name ; //echo january
thanks in advance
The PHP date () function can take different types of format and display date depending on that format. Using the date () function, we can also show the month name in PHP. To show the month name on the web page using the date function, we just have to pass a particular format inside the function.
Method 1: Using date () function to retrieve current month PHP’s date () function can let you know date and time related information based on the formatting characters it takes in its first parameter. The function can take maximum of two parameters. If you use only one parameter, It will return information related to current time.
If you have the month number, you can first create a date from it with a default date of 1st and default year of the current year, then extract the month name from the date created: Easiest and most straightforward solution. $monthNum = 5; $monthName = date ("F", mktime (0, 0, 0, $monthNum, 10));
$monthNum = 3; $monthName = date ('F', mktime (0, 0, 0, $monthNum, 10)); // March If you want the 3-letter month name like Mar, change F to M. The list of all available formatting options can be found in the PHP manual documentation. This doesn't respect the LC_TIME locale and will always output the month name in English.
echo date('F', strtotime("2012-$int-01"));
Another built in way would be
$monthInt = 3;
$monthName = DateTime::createFromFormat('m', $monthInt)->format('F');
I would be nice if PHP had a built in way of getting date names without creating a date object.
Use this code to get month name by giving an integer value PHP
<?php
function get_month_name($inp)
{
return date("F", strtotime(date("d-$inp-y")));
}
$month_name = get_month_name("1");
echo $month_name;
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With