PHP Date Exercises : Number of days of the current month
27. Days in Current Month
Write a PHP script to get the number of days of the current month.
Sample Solution:
<?php
// Output the number of days for the current month
echo 'Number of days for the month of '.date('M'). ' is :' .date('t')."\n";
?>
Output:
// Output the number of days for the current month echo 'Number of days for the month of '.date('M'). ' is :' .date('t')."\n";
N.B.: The result may varry for your system date and time.
Explanation:
In the exercise above,
echo 'Number of days for the month of '.date('M'). ' is :' .date('t')."\n";: Outputs the current month's name concatenated with the total number of days in the month obtained using the "date()" function. The format specifier 'M' returns the abbreviated month name, and 't' returns the number of days in the given month.
For more Practice: Solve these Related Problems:
- Write a PHP script to calculate the number of days in the current month using built-in date functions.
- Write a PHP function that returns the total days of any given month and year, and test it with leap year scenarios.
- Write a PHP program to display the current month’s name along with the number of days it contains, formatted in a sentence.
- Write a PHP script to output the number of days in the current month by creating a DateTime object and using the format('t') method.
Go to:
PREV : Convert Number to Month Name.
NEXT : Display Time in Specified Timezone.
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.