I've got a pesky date problem...

I send a string from a form on one page that provides YEAR and MONTH info (GET method)

and it seems to receive the data into the target script just fine:

Example the target script gets this

$MONTHFILTER = $_GET['MONTHFILTER'];

If the value is "2005-01"
and I
echo "$MONTHFILTER";

I see "2005-01" just like I would expect.

But I want to see the month name for this value at certain places.

So I try the basic method of:

$month = date('F', substr($MONTHFILTER, 5, 2));

but the statement

echo "$month";

always outputs "December"

Anybody? Thanks.

    I hit on something that works, but would welcome critical comments:

    I found I have to use the mktime function:

    First, extract the month number...

    $month = date('F', substr($MONTHFILTER, 5, 2));

    THEN I can use mktime (nested in the date formatting function) to get the proper month name.

    $monthname = date("F", mktime(0,0,0,$month+1,00,$year));

    Why must I add "+1" to $month to get the proper month? Does PHP think January is "00"?

    Should I delete message threads where I find my own answers?

      It might be easier and clearer if you declared an array of month names, and then used that from the month values.

        Thank you. That's a good idea. But when it comes to codig I am becoming a believer in the adage "The lazy man will find the best way"...if there is a function that I can understand well enough to save me some paragraphs of code, I'm all in favor of it! 😉

        I think I get the "$month+1" thing:

        The "00" in the day position of the mktime arguement that follows $month, is interpreted as a virtual day at the end of the prior month...as if a month could be longer than it actually is, thus, 01,00 would be understood as December, while 01,01 would be understood as January (and 01,32 would be understood as February).

          if there is a function that I can understand well enough to save me some paragraphs of code, I'm all in favor of it!

          Your function probably is less efficient than the array, and what saves you just a little more code now will save you (or future maintainers of that code) much less time spent trying to figure out what exactly you did.

            Well to answer your questions, the reason you have to use mktime() is because date() figures out the date based on a Unix timestamp.

            As far as why you have to add one to the month, that's explained in the manual. You have to give one for the date, or else it gives you the last day of th previous month, because there is no January 0, 2005. So instead, you get December 31, 2004.

            You should use:

            mktime(0, 0, 0, $month, 1, $year);

            Hope it helps.

              Write a Reply...