w3resource

PHP Date Exercises : Change month number to month name

PHP date: Exercise-12 with Solution

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

PHP Code Editor:

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

Previous: Write a PHP script to get time difference in days and years, months, days, hours, minutes, seconds between two dates.
Next: Write a PHP script to get yesterday's date.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php-exercises/php-date-exercise-12.php