Hope you're all enjoying Presidents Day; I have nothing presidential to post to mark this occasion, so I hope this will do ...

A common requirement in programming is determining the number of days in a given month. To do this, many of us will try to utilize date/time functionality built-into the language. Others will recite the "Thirty Days Hath September" poem and hard-code the days, braving future compatibility problems (just ask a vet how much of a pain the Julian Migration was back in 45). And then there are the few who just can't seem to find a good way of doing this, and come up with their own way. Like Michael Knight's colleague, who'll just keep picking a number until it works ...

//return the days in the month for a given month/year
function daysInMonth($thisMonth,$thisYear) {
 $daysInMonth=1;
 while (checkdate($thisMonth,$daysInMonth,$thisYear)):
  $daysInMonth++;
 endwhile;
 return $daysInMonth;
}

Of course checkdate() is a built-in function that simply indicates if a date is valid. And on the subject of built-in PHP date/time functions ... I wonder what the functional specifications discussion was like for debating whether or not to include date_sunrise() and date_sunset().

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!