w3resource

PHP Date Exercises : Change month number to month name


12. Convert Month Number to Month Name

Write a PHP script to change month number to month name.

Sample Solution:

PHP Code:

<?php
$month_num = 7; // Assigning the month number 7 to the variable $month_num.
$month_name = date("F", mktime(0, 0, 0, $month_num, 10)); // Generating the month name using the mktime function.
echo $month_name."\n"; // Outputting the month name.
?>

Output:

July

N.B.: The result may varry for your system date and time.

Explanation:

In the exercise above,

  • $month_num = 7: Assign the month number 7 (July) to the variable '$month_num'.
  • $month_name = date("F", mktime(0, 0, 0, $month_num, 10));: Use the "mktime()" function to generate a Unix timestamp for the specified month (July) and day (10). The "date()" function with the format specifier "F" converts this timestamp into the full month name.
  • echo $month_name."\n";: Outputing the month's name followed by a newline character.

Flowchart :

Flowchart: Change month number to month name

For more Practice: Solve these Related Problems:

  • Write a PHP function that takes a numeric month (1-12) as input and returns its full month name.
  • Write a PHP script to convert an array of month numbers into an array of corresponding month names using a lookup table.
  • Write a PHP program to display the current month’s name by converting the numeric value from date('n').
  • Write a PHP function to convert a given month number to its abbreviated name and verify against a predefined list.

Go to:


PREV : Time Difference in Detailed Units.
NEXT : Get Yesterday's Date.

PHP Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.