Month number increases but month name does not

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
C ould someone please explain this one? This one got me:

PHP Code:
for ($thisMonth = (int)$minMonth; $thisMonth <= 12; $thisMonth++) {
       $monthName = date('M', strtotime($thisMonth));
       print_r("thisMonth = $thisMonth and monthName = $monthName<P>");

thisMonth = 7 and monthName = Sep

thisMonth = 8 and monthName = Sep

thisMonth = 9 and monthName = Sep

thisMonth = 10 and monthName = Sep

thisMonth = 11 and monthName = Sep

thisMonth = 12 and monthName = Sep

I am at a loss, please help, time sensitive I'm afraid, must be done in
an hour (it's 4pm EST now)


Re: Month number increases but month name does not

comp.lang.php wrote:

Quoted text here. Click to load it

OK... first, why do you say (int)$minMonth?  Is casting really necessary?
Somehow I doubt it.

Your problem is that you are passing $thisMonth to strtotime().  That
function expects the description of *a complete date*, not just one date
element such as a month.  You are passing, e.g. "4" to strtotime(), and it
has absolutely no idea what that 4 is supposed to refer to.

There are two potential solutions that I can think of:

Create a dummy date to pass to strtotime.  For example,


returns a date of the fourteenth day of whichever month you're using.  Since
you're only worried about the value of the month, the day and year don't
matter; they just need to be present (and valid) in the call to strtotime().

The second, and IMO better, solution is to stop abusing strtotime() and just
write your own function:

  function get_month_name($monthNumber) {
      $months = array(
          1 => 'January',
          2 => 'February',
          12 => 'December'
       return $months[$monthnumber];

Then you can just use

  $monthName = get_month_name($thisMonth);

Benjamin D. Esham  |  AIM: bdesham128  |  Jabber: same as e-mail
"Conservative, n.  A statesman enamored of existing evils, as
opposed to a Liberal, who wants to replace them with new ones."
                      — Ambrose Bierce, /The Devil's Dictionary/

Site Timeline