Day In Month and Is this a leap year variation

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Day In Month and Is this a leap year variation

Post by mchaggis »

Code: Select all

<?php
function DaysInMonth( $month=0, $year=0 ) {
    // Sanatise the passed vars
    $month = intval($month);
    $year = intval($year);

    // If the year wasn't specified, then use current year
    if (!$year) $year = date("Y");

    // If the month wasn't specified, then use current month
    if (!$month) $year = date("n");

    // Return the days in the month
    return date("j", mktime(0,0,0,$month,0,$year));
}

function IsLeapYear( $year=0 ) {

    if ( date("j", mktime(0,0,0,3,0,$year)) == 29 ) {
        return TRUE;
    } else {
        return FALSE;
    }

}

?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Days in month can be found by using date('t',$unix_timestamp)

Finding if it's a leap year can be found by using date('L',$unix_timestamp)

In both cases, $unix_timestamp is, of course, a UNIX timestamp (found by calling time() or mktime(......))
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

You could just use checkdate() to validate it.
Post Reply