How to change month number to month name in PHP

In this article, we will discuss how to change the month number to month name using PHP. It can be done in many ways, from that following methods are covered in this article,

  • using the date()  method.
  • using the gregoriantojd() method.
  • using the DateTime class object.

How to change month number to month name using date() in PHP

In PHP, the date()  method converts the number to index of the month and display its corresponding month name.

The following script demonstrates the use of date() method to retrieve the month name,

<?php
//month input in value
$month_val = 7;//if you input >12 then it will complete a cycle and return month name

//creating name with date function
$mon_name = date("F", mktime(0, 0, 0, $month_val, 10));

//display month name
echo $mon_name."\n"; 
?>

Output:-

July

How to change month number to month name using gregoriantojd()

In PHP, the gregoriantojd()  method fetches the month number and converts to its corresponding month name.

The following script demonstrates the use of gregoriantojd() method to retrieve the month name,

<?php

//convert date to georgian time
$jdate=gregoriantojd(7,13,1998);

//fetch month name from the above date and display
echo jdmonthname($jdate,0);
?>

 

Output:-

Jul

How to change month number to month name using the DateTime class

In PHP, the month name can be retrieved by creating the object DateTime class.

The following script that illustrates the use of DateTime class to retrieve month name,

<?php

  //month in value
    $month_val  = 7;
  
  //creating object of DateTime and fetching the month
  $dbj   = DateTime::createFromFormat('!m', $month_val);
  
  //Format it to month name
  $mName = $dbj->format('F');
  
  //display the month name
  echo $mName;
?>

Output:-

July

This is how we can convert month number to month name using PHP. If you have any doubts about this article, leave a comment below.

See also,

Leave a Reply

Your email address will not be published. Required fields are marked *