Calculate How Many Days in A Month Using PHP

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
  1. <?php  
  2. $cal = cal_days_in_month(CAL_GREGORIAN, 5, 2013);  
  3. echo "There was $cal days in may 2013";  
  4. ?>  
Output
 
cal1.jpg
 
Example
  1. <?php  
  2. $cal = cal_days_in_month(CAL_GREGORIAN, 11, 2011);  
  3. echo "There was $cal days in november 2011";  
  4. ?>  
Output
cal2.jpg
 
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
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />  
  5. <title>How many days in the month</title>  
  6. </head>  
  7. <body>  
  8. <?php  
  9. date_default_timezone_set('america/new_york');  
  10. $mnth_name = date('F');  
  11. echo '<p>The month is '$mnth_name .'</p>';  
  12. echo '<p> Therefore ';  
  13. $mnth = date('n');  
  14. if($mnth==1)  
  15. {  
  16. echo 'days is 31';  
  17. }  
  18. if($mnth==2)  
  19. {  
  20. echo 'days is 28 unless it is a leap year';  
  21. }  
  22. if($mnth==3)  
  23. {  
  24. echo 'days is 30';  
  25. }  
  26. if($mnth==4)  
  27. {  
  28. echo 'days is 30';  
  29. }  
  30. if($mnth==5)  
  31. {  
  32. echo 'days is 31';  
  33. }  
  34. if($mnth==6)  
  35. {  
  36. echo 'days is 29';  
  37. }  
  38. if($mnth==7)  
  39. {  
  40. echo 'days is 31';  
  41. }  
  42. if($mnth==8)  
  43. {  
  44. echo 'days is 31';  
  45. }  
  46. if($mnth==9)  
  47. {  
  48. echo 'days is 30';  
  49. }  
  50. if($mnth==10)  
  51. {  
  52. echo 'days is 31';  
  53. }  
  54. if($mnth==11)  
  55. {  
  56. echo 'days is 30';  
  57. }  
  58. if($mnth==12)  
  59. {  
  60. echo 'days is 31';  
  61. }  
  62. ?>  
  63. </body>  
  64. </html>  
Output
cal.jpg


Similar Articles