Skip to content

Instantly share code, notes, and snippets.

@SoboLAN
Created March 30, 2017 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SoboLAN/72d3c5c6209ed654e1dfb5c404bb8c95 to your computer and use it in GitHub Desktop.
Save SoboLAN/72d3c5c6209ed654e1dfb5c404bb8c95 to your computer and use it in GitHub Desktop.
Get number of days in a month
<?php
function isLeapYear($year)
{
return $year % 100 == 0
? $year % 400 == 0
: $year % 4 == 0;
}
function getMonthDayCount($year, $month)
{
switch ($month) {
case 'april':
case 'june':
case 'september':
case 'november':
return 30;
case 'january':
case 'march':
case 'may':
case 'july':
case 'august':
case 'october':
case 'december':
return 31;
case 'february':
return (isLeapYear($year) ? 29 : 28);
default:
return 0;
}
}
var_dump(getMonthDayCount(2017, 'march')); //prints 31
var_dump(getMonthDayCount(2000, 'february')); //prints 29
var_dump(getMonthDayCount(1999, 'december')); //prints 31
var_dump(getMonthDayCount(1600, 'june')); //prints 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment