PHP MD5() a strange thing!

The other day I came across a strange thing, or at least I thought it was a strange thing.

Whilst writing a small application that would migrate user accounts from one database to another, I encountered one user who could log in quite happily without entering a password at all. Read More >

Use PHP to get the number of days in a month

PHP has a cool way to get the number of days in a given month. I bet you have all used this function and not known you could do this.

The function is the good old date function.

date();

So we have all used the date function to get today’s date and convert it to what ever format you choose.

For instance

date('dF m Y');

Would give us something like 12th June 2011.

Lets take it one step further and work out how many days there are in this month.

echo date('t',strtotime('june2011'));

This would give us 30, because that’s how days there are in June this year.
So what are we doing here. Lets break it down.

PHPDate

What’s the time in our New York office?

If you are lucky enough to work for a large company which has offices all over the world you probably need your applications to know about and respect local timezones.

There seems little point having your application perform all kinds of cool wizardry if the target audience are all in bed because it’s 4am over there.

Thankfully PHP offers a simple way to set the local timezone. I’ll leave it up to you to decide which user is in which timezone.

Read More >

Get the current Quarter number with PHP

I was recently tasked with obtaining the current quarter number.

For those who aren’t sure, the quarter number for January through March is Q1, April through June is Q2 and so on.

Read More >

Quickly format a number in PHP

The other day I came accross a situation where I had a number with a huge amount of decimal places like this.

199.94649675629

This looks just dirty and nasty and horrible so obviously it needed formatting. My first thought was to do something like this.

//The long number
$LongNumber = "199.94649675629";

//The position of the decimal point (or a dot)
$Pos = strpos($LongNumber,".");

//Find everything from the start of the string to the 
//position of the dot plus three places
$NewNumber = substr($LongNumber, 0, $Pos+3);

echo $NewNumber;

That worked fine but after some thought I decided that it wasn’t the best way to go at all. So lets look at a much faster way to perform the same functionality.

$LongNumber = "199.94649675629";
$LongNumber = number_format($LongNumber , 2);

Now that’s much better isn’t it?

How the function works

The number_format function accepts one, two or four parameters. Most of the time you will only need the first two parameters.

Parameter 1 is the long number that you want to format.
Parameter 2 is the number of decimal places you want to show.

If you want to specify the thousands separator then you can but you MUST specify two further parameters.
Parameter 3 is the decimal point which is used to find the position in order to format.
Parameter 4 is the thousands separator. Generally this is set to a comma “,” but you could get rid of it entirely by specifying “” as in an empty string.

Any thoughts on this? Ways to improve it, I’m always looking to improve.