Diary of a geek

November 2005
Mon Tue Wed Thu Fri Sat Sun
 
10
       

Andrew Pollock

Categories

Other people's blogs

Subscribe

RSS feed

Contact me

JavaScript required


Thursday, 10 November 2005

Distributed code reviews are cool

Thanks Sam for your even more compact (but harder to follow) PHP code to find a specific instance of a given day in the month

I haven't tested your first function rigorously, but it seems to hold up to the second and fourth Thursdays of the month, which is mainly what I'm after.

The second function, which avoids function calls, appears to suffer from a partial off-by-one bug.

[13:33] [code] [permalink]

And it's not even summer yet!

My friend Kim, who's done the Brisbane → Melbourne → Brisbane thing (like I've done the Brisbane → Canberra → Brisbane → Canberra thing) is finding summer back in Brisbane a bit uncomfortable.

When I moved back to Brisbane I did it around this time of year, and coming from Canberra's late spring (which actually behaves like a late spring) to Brisbane's eternal summer of a spring is a bit of a shock to the system.

The humidity was the prime factor in me wanting to return to the blissful dry heat of Canberra's summer.

Kim, I feel your pain, furthermore, I'll be in Brisbane this weekend to feel it in person.

[03:45] [life] [permalink]

Finding a specific instance of a given day in the month in PHP

When I took over organising the CLUG meetings, I managed to replace most of myself with a small shell script (I even have the t-shirt).

Now that I'm leaving Canberra, Steve Walsh has kindly taken over the running of the script.

So I've done a bit more work on the script, and added a public front end to it, and made an RSS feed (my very first).

So until yesterday, to work out the fourth Thursday of the month, I'd been calling a Perl script that used Date::Manip, when I decided to investigate doing it with PHP natively. Tony gave me some initial code, but I ended up with this:


function nth_day($instance, $dow, $month, $year)
{
    
$first_dom = date("w", mktime(0, 0, 0, $month, 1, $year));
    
    if (
$first_dom <= $dow) {
        
$first_instance = date("j", mktime(0, 0, 0, $month, 1 + ($dow - $first_dom), $year));
    } else {
        
$first_instance = date("j", mktime(0, 0, 0, $month, 8 - ($first_dom - $dow), $year));
    }

    
$instance_we_want = $first_instance + (($instance-1) * 7);

    
$date = date("j", mktime(0, 0, 0, $month, $instance_we_want, $year));

    return
$date;
}

[02:05] [code] [permalink]