PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
It is currently Tue Aug 11, 2020 1:57 pm

All times are UTC - 5 hours

Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sat Jan 07, 2012 9:41 pm 
Forum Regular
User avatar

Joined: Tue Sep 28, 2010 11:41 am
Posts: 984
Location: Columbus, Ohio
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 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 ]
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]);
            $retval = $aryMonths[$month];
        return ($type==GET_MONTH_SHORT) ? substr($retval,0,3) : $retval;

Last edited by twinedev on Sun Jan 08, 2012 3:51 am, edited 1 time in total.

PostPosted: Sun Jan 08, 2012 3:36 am 
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
Thanks for posting this code. I may be helpful to someone.

Moved to Coding Critique.


Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours

Who is online

Users browsing this forum: No registered users and 8 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group