Introduction
In this article I explain the "cal_days_in_month()" function in PHP. This function returns the number of the days in a month for the given year and calendar.
Use of this function
You can easily determine how many days are in the month and you can determine the days of any month.
Syntax
Cal_days_in_month(calendar , month , year); |
Parameter |
Description |
Calendar |
Calendar to use for calculation. |
Month |
Month in select calendar. |
Year |
Year in select calendar. |
Returns the length in days of the selected month in the given calendar.
Example
- <?php
- $cal = cal_days_in_month(CAL_GREGORIAN, 5, 2013);
- echo "There was $cal days in may 2013";
- ?>
Output
Example
- <?php
- $cal = cal_days_in_month(CAL_GREGORIAN, 11, 2011);
- echo "There was $cal days in november 2011";
- ?>
Output
Next I will explain how many days are in the month without using any calendar and determine the days used by the date function. But you will use these techniques to determine the current days of the month.
Example
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <title>How many days in the month</title>
- </head>
- <body>
- <?php
- date_default_timezone_set('america/new_york');
- $mnth_name = date('F');
- echo '<p>The month is '. $mnth_name .'</p>';
- echo '<p> Therefore ';
- $mnth = date('n');
- if($mnth==1)
- {
- echo 'days is 31';
- }
- if($mnth==2)
- {
- echo 'days is 28 unless it is a leap year';
- }
- if($mnth==3)
- {
- echo 'days is 30';
- }
- if($mnth==4)
- {
- echo 'days is 30';
- }
- if($mnth==5)
- {
- echo 'days is 31';
- }
- if($mnth==6)
- {
- echo 'days is 29';
- }
- if($mnth==7)
- {
- echo 'days is 31';
- }
- if($mnth==8)
- {
- echo 'days is 31';
- }
- if($mnth==9)
- {
- echo 'days is 30';
- }
- if($mnth==10)
- {
- echo 'days is 31';
- }
- if($mnth==11)
- {
- echo 'days is 30';
- }
- if($mnth==12)
- {
- echo 'days is 31';
- }
- ?>
- </body>
- </html>
Output