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
    7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should...

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

Incredible Demos

  • By
    MooTools 1.2 OpenLinks Plugin

    I often incorporate tools into my customers' websites that allow them to have some control over the content on their website. When doing so, I offer some tips to my clients to help them keep their website in good shape. One of the tips...

  • By
    Scroll IFRAMEs on iOS

    For the longest time, developers were frustrated by elements with overflow not being scrollable within the page of iOS Safari.  For my blog it was particularly frustrating because I display my demos in sandboxed IFRAMEs on top of the article itself, so as to not affect my site's...

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!