Sample Code: Month Handling

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Sample Code: Month Handling

Post by twinedev »

I'm working on a script that deals with dates a lot, and needs a variety of formats, as well as the output uses various formats allowed by the client's template handler. The converting back and forth was getting repetitive throughout the code, so I just wrote the following function, which you pass the month, as a number, full spelled out word, or abbreviated word.

It will then return, depending on what settings you pass it, an integer value of the month, a 2 digit padded string of the month's number, the full month name, abbreviated month name, each of those as either Camel case, lower case, or upper case.

For small use, yes you can just use the date() function, but this has a lot of date checking/manipulation, not to mention that output is via a template where the client (currently) has month displaying various ways.

An example: client wants URLs in format of http://example.com/reports/January_2010 so mod_rewrite is doing /index.php?month=January&year=2010. Now at the top of the script I can do: $intMonth = (isset($_GET['month'])) ? getMonth($_GET['month'],GET_MONTH_NUMBER) : FALSE;

Again, there are other ways of doing this, but with the many variations and back and forth needing to be done in different parts, I chose this as it sets up an array to work off of compared to a bunch of conditional date()calls throughout.

Code: Select all

define('GET_MONTH_FULL',1);
define('GET_MONTH_SHORT',2);
define('GET_MONTH_NUMBER',4);
define('GET_MONTH_PADDED',5);
define('GET_MONTH_UPPER',1);
define('GET_MONTH_LOWER',2);
define('GET_MONTH_CAMEL',3);
function getMonth($month,$type=1,$style=3) {
    static $aryMonths = NULL;
    if (is_null($aryMonths)) {
        $aryMonths = array();
        for($m=1;$m<=12;$m++) {
            $strLong = date('F',mktime(0,0,0,$m,1,1972));
            $aryMonths[strtolower(substr($strLong,0,3)] = $m;
            $aryMonths[strtolower($strLong)] = $m;
            $aryMonths[$m] = $strLong;
        }
    }
    if ((int)$month>0) {
        if ($month<1 || $month>12) { return FALSE; }
        $month = (int)$month;
    }
    else {
        $month = strtolower($month);
        if (!array_key_exists($month,$aryMonths)) { return FALSE; }
        $month = $aryMonths[$month];
    }
    
    // $month is a valid integer of the month now
    
    if ($type==GET_MONTH_NUMBER)
        return $month;
    elseif ($type==GET_MONTH_PADDED)
        return str_pad($month,2,'0',STR_PAD_LEFT);
    else {
        if ($style==GET_MONTH_UPPER)
            $retval = strtoupper($aryMonths[$month]);
        elseif ($style==GET_MONTH_LOWER)
            $retval = strtolower($aryMonths[$month]);
        else
            $retval = $aryMonths[$month];
            
        return ($type==GET_MONTH_SHORT) ? substr($retval,0,3) : $retval;
    }
}
Last edited by twinedev on Sun Jan 08, 2012 2:51 am, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Sample Code: Month Handling

Post by Christopher »

Thanks for posting this code. I may be helpful to someone.

Moved to Coding Critique.
(#10850)
Post Reply