You are here: Re: Month number increases but month name does not « PHP Programming Language « IT news, forums, messages
Re: Month number increases but month name does not

Posted by Benjamin Esham on 09/01/06 21:44

comp.lang.php wrote:

> Could someone please explain this one? This one got me:
>
> for ($thisMonth = (int)$minMonth; $thisMonth <= 12; $thisMonth++) {
> $monthName = date('M', strtotime($thisMonth));
> print_r("thisMonth = $thisMonth and monthName = $monthName<P>");
> }

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,

strtotime("$thisMonth/14/06")

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);

HTH,
--
Benjamin D. Esham
bdesham@gmail.com | 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/

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация