Created
March 30, 2017 08:47
-
-
Save SoboLAN/72d3c5c6209ed654e1dfb5c404bb8c95 to your computer and use it in GitHub Desktop.
Get number of days in a month
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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