How to get the correct month name using PHP

3:07pm, 31st October 2007

Can you see anything wrong in the following image?

Screenshot of james.lab6.com showing mktime bug

Persistent readers will notice that it’s part of the sidebar on this site’s main page, except slightly wrong. It turns out I have a bug that only occurs at the end of months! The following code is wrong:

date('F', mktime(0,0,0,$month))

The following code is right:

date('F', mktime(0,0,0,$month,1))

Why? It’s down to PHP being clever, and me being dumb (or possibly the other way round; it’s hard to tell with PHP).

The mktime function constructs a timestamp from the arguments you give it, filling in any missing arguments with the current local date and time. I guess this might be useful in some circumstances, but if you call it on, say, the 31st of October, when trying to use it to map 9 to September, the mktime function uses 31 for $day, and clearly there’s no such time as the 31st of September, so PHP decides to be clever by overflowing to the 1st of October. Similarly, attempting to map 2 to February when on the 31st of October overflows to the 3rd of March. Lovely.

The fix is to specify 1 for the day, since there’s always a first of the month.

The number one search result for “php get month name” currently recommends the wrong code. Google’s Code Search shows that this flawed idiom has already infiltrated a number of open source projects.

But now for the important part: there is a fixed version of my Wordpress theme!


Poverty is not a virtue

4:53pm, 31st October 2007

I will declare thy righteousness, and thy works; for they shall not profit thee. [Isiah 57:12]

I have seen the light, and I no longer use a Creative Commons license which prohibits commercial use. I now use the simpler, newer Attribution-Share Alike 2.0 UK license. Yay!

My primary reason for using -NC in the first place was to guard against the possibility of Bad Guys ripping off content and making it un-Free. I now realise that the share-alike clause already protects against such Bad Guys, except the ones who don’t care about licenses, who won’t be deterred by any license clause.

My new primary reason for abandoning -NC is that I don’t believe there is any such thing as non-commercial use. Sure, there are legal definitions of non-profit and such, but they suffer from the idea that money is the same thing as value, or wealth. If a random blogger wants to use one of my photos on their AdSense-enabled blog, they might make money, but even if they don’t run ads, they still create wealth. Why? Because they wouldn’t have used the picture unless they perceived some value in doing so! It could make their blog prettier, attract more viewers, generate peer cred, or even just make them happy. Money is one type of wealth among many, and I like the idea that my photo could create wealth for someone. Wealth is a good thing, in whatever form it takes, including money.

It seems picky and unjustifiably miserly to say that someone can use my photo to create all kinds of wealth except money, especially since money is just an intermediate form of the other kinds of wealth.

Share and enjoy!