PHP Date Exercises : Change 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 :
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics