PHP Function – Calculating Days In A Month

By  on  

All developers keep a tool box of useful functions and classes that they pick up or write along the way and this PHP came to me while I was writing an event calendar page for a customer.

I should mention that PHP does offer a cal_days_in_month function for PHP builds of PHP 4.0.7 and higher, but I prefer using the below function because it is guaranteed to work on all PHP version because it is based solely on logic.

The Code

function get_days_in_month($month, $year)
{
   return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year %400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
}

One line of logic provides the number of days in a month, taking into account leap years, and all on one line.

Recent Features

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

Incredible Demos

  • By
    Spatial Navigation

    Spatial navigation is the ability to navigate to focusable elements based on their position in a given space.  Spatial navigation is a must when your site or app must respond to arrow keys, a perfect example being a television with directional pad remote.  Firefox OS TV apps are simply...

  • By
    Highlighter: A MooTools Search &#038; Highlight Plugin

    Searching within the page is a major browser functionality, but what if we could code a search box in JavaScript that would do the same thing? I set out to do that using MooTools and ended up with a pretty decent solution. The MooTools JavaScript Class The...

Discussion

  1. noseyparker

    Thanks for your very helpful site! Love the tips.

  2. Michael Quinn

    Hi David,

    PHP versions 4&5 also offer the date("t") function which returns number of days in given month.

  3. michael is right: $totDays = date("t",strtotime($year.'-'.$_month.'-01'));

  4. LOL.. Thanks Michael and Paskuale but I think you’re both missing the point! It’s nice to be told that we can get the days directly but the point about this function is that it’s an easily recognisable way to learn how ternary functions in PHP.

    Very useful for noobies such as myself – thanks David :)

  5. LOL.. Thanks Michael and Paskuale but I think you’

    LOL.. Thanks Michael and Paskuale but I think you’re both missing the point! It’s nice to be told that we can get the days directly but the point about this function is that it’s an easily recognisable way to learn how ternary functions in PHP.

    Very useful for noobies such as myself – thanks David :)

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!