PHP Developers Network
http://forums.devnetwork.net/

Sample Code: Month Handling
http://forums.devnetwork.net/viewtopic.php?f=50&t=133819
Page 1 of 1

Author:  twinedev [ Sat Jan 07, 2012 9:41 pm ]
Post subject:  Sample Code: Month Handling

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.

Syntax: [ Download ] [ Hide ]
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;
    }
}

Author:  Christopher [ Sun Jan 08, 2012 3:36 am ]
Post subject:  Re: Sample Code: Month Handling

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

Moved to Coding Critique.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/