Finally learning PHP

Saturday 24th January 2009, 16:29

Tagged: Code PHP

Well, the other day, after reading the basics in books and on the net, I wrote my first little PHP script. It isn't much really, but I'm still really happy with how it's turned out. What it does, is tell you the day you were born, based on the date. So for example, 17 June 1991 (my birthday) was a Monday, and if I put that into the form, it will tell me that. Magic.

Well no it isn't magic, the main workings of it is just a mathematical formula that I was taught at college, but I thought that would be a good thing to write a script on. The script itself is relatively basic, it just takes the values from the input form, assigns them to variables, does some stuff with those, then puts it through the calculation to get a number from 0 to 6 (Sunday to Saturday). Then I wrote out a load of if, elseif and else statements to show the actual day. I also put in some statements for if you put in a day that doesn't exist, (30/31st Feb, 31st Apr, Jun, Sep, Nov), if you try and select 29th Feb in a non-leap year, and also if you try and select 29th Feb in a non-century leap year (a century year is only a leap year if divisible by 400, so 1200, 1600 and 2000 were century leap years). So, although it's quite basic, it's been a big learning curve for me, and I've learned a lot from such a small script, especially how to get information from a form, and it helped me understand ifelseif and else statements a lot more.

The script itself is at least 99% accurate. There's not many dates that won't work, and the ones that don't are either hundreds of years ago, or in 2000+. I'm not too sure why it breaks with years that are 2000+, I think the formula ends up having to divide by 0 with those years, and dividing by 0 isn't mathematically possible.

I'm probably making a big deal out of something quite pathetically small but it's the first thing I've ever written that actually works so I'm quite pleased.

You can test the most stable version, with years from 2000 onwards disallowed, here. To see the one that allows years that are 2000 onwards, see here.

Hopefully now I've started to understand this stuff more, I'll be able to do much more, much quicker.

Saturday 24th January 2009, 16:29

Words: 400

Tagged: Code PHP

Add a Comment

Remember me?

Hide my name?