CodeIgniter Forums
get_total_days (function) in Calendar class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: get_total_days (function) in Calendar class (/showthread.php?tid=7187)



get_total_days (function) in Calendar class - El Forum - 03-28-2008

[eluser]Crucial[/eluser]
I just started using the calendar class for an accounting/finance type application, and was wondering why the get_total_days function used so much code for something that's already built into PHP?

Current PHP Code
Code:
function get_total_days($month, $year)
{
    $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

    if ($month < 1 OR $month > 12)
    {
        return 0;
    }

    // Is the year a leap year?
    if ($month == 2)
    {
        if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
        {
            return 29;
        }
    }

    return $days_in_month[$month - 1];
}

Modified PHP Code
Code:
function get_total_days($month, $year)
{    
    if ($month < 1 OR $month > 12)
    {
        return 0;
    }
    
    return date('t', mktime(0, 0, 0, $month, 1, $year));
}