Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to get month name by giving an integer value PHP

Tags:

php

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

like image 352
Kanishka Panamaldeniya Avatar asked Mar 15 '12 07:03

Kanishka Panamaldeniya


People also ask

How to show the month name in PHP?

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.

How to retrieve current month using PHP date() 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.

How to get the month number and month name from date?

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));

How to get the 3-letter month name in PHP?

$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.


3 Answers

echo date('F', strtotime("2012-$int-01"));
like image 97
deceze Avatar answered Oct 16 '22 07:10

deceze


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.

like image 37
Rowan Avatar answered Oct 16 '22 07:10

Rowan


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;
  ?>
like image 4
Balaji Kandasamy Avatar answered Oct 16 '22 08:10

Balaji Kandasamy