Posts about the server side scripting language PHP

PHP Logo

Converting To And From Decimal Time In PHP

29th November 2010 - 3 minutes read time

To convert a time value into a decimal value representing the number of minutes can be useful for certain calculations. The following function takes a time as a string of hh:mm:ss and returns a decimal value in minutes.

 * Convert time into decimal time.
 * @param string $time The time to convert
 * @return integer The time as a decimal value.
function time_to_decimal($time) {
    $timeArr = explode(':', $time);
    $decTime = ($timeArr[0]*60) + ($timeArr[1]) + ($timeArr[2]/60);

    return $decTime;

If we take the time of 11:11:11 this gets split into 3 parts by the explode() function into hours, minutes and seconds, which then gets treated in the following way:

Minutes = (Hours x 60) + (Minutes) + (Seconds / 60)
Minutes = (11 x 60) + (11) + (11 / 60)
Minutes = (660) + (11) + (0.18333333)
Minutes = 671.18333333

The function can be used as follows:

PHP Logo

PHPNW10: A Review

11th October 2010 - 37 minutes read time

This weekend saw the 3rd PHPNW conference and being a PHP developer, working in Manchester, it would be inexcusable for me not to attend :). After missing my train and pouring my first coffee of the day into my conference pack it wasn't the best of starts. However, I still managed to turn up in plenty of time so I didn't miss any of the talks and got to say hello to the people I know from the PHPNW user group and some who I met at the PHPNW conference in previous years.

Day 1

Keynote: Teach a Man to Fish: Coaching Development Teams
Lorna Mitchell @lornajane

PHP Logo

PHP TestFest UK 2010 And Testing PHP

29th September 2010 - 11 minutes read time

The UK PHP TestFest this year was held at MadLab in Manchester on the 11th September. I was one of the 12 people who went along to learn about testing the PHP language. So I thought that I would collate some of the things that we went through during the session.

Before creating a test you need to set up your testing environment, you can do this by going to the TestFest site and running through the tutorial on setting up your system for testing PHP. When you have finished setting up your test environment you will have a folder containing three folders, these are php52, php53 and php-trunk, these are the different versions of PHP which you can test, although we will be concentrating on the php53 version in this article the same practices can be used for the other trunks.

PHP Logo

Print Image With Fixed Aspect Ratio In PHP

20th September 2010 - 3 minutes read time

When displaying images added by users it is quite often necessary to resize them in order that they fit into a specific area of the page. Not doing this can cause problems with the images breaking the page layout. The trouble is that if you resize the image absolutely you tend to squash and distort it.

The following function can be used to calculate the width and height of an image to the correct aspect ratio, which will preserve the contents when printed out. This function makes use of the getimagesize() function, which is available as part of the GD library in PHP.

PHP Logo

PHP Version Number

25th June 2010 - 3 minutes read time

Different functions and options are always being added to PHP. Although new versions generally don't create much backward compatibility issues it is usually prudent to write production code that you know will work on servers running a slightly older version of the language.

To check the currently used PHP version you can use the function phpversion() or the constant PHP_VERSION. Both the function and the constant return a string that contains the version number. There are a couple of ways in which this information can be used, the first is to take the string and convert into an array using the explode() function. With this array you can then check the minor version number and run code like the following:

PHP Logo

Find A Month From A Given Integer With PHP

7th June 2010 - 1 minute read time

If you need to know the month from a given integer (from 1 to 12) then you can use the following snippet. This will return the string "Feb".

date("M", mktime(0, 0, 0, 2));

This can be encapsulated into a function call that will take a number between 1 and 12 and return the corresponding string for that month. This function includes some simple error checking to make sure that the number is valid before trying to work out the date.

function getMonth($month) 
  if (!is_numeric($month) && $month  12) {
    return false;
  return date("M", mktime(0, 0, 0, $month));
PHP Logo

Parsing XML with PHP

6th May 2010 - 11 minutes read time

XML data extraction can be a common task, but to work directly with this data you need to understand how PHP parses XML. There are various different functions involved in parsing XML in PHP, all of which work together to extract data from a XML document. I will go through each of these functions and tie them together at the end.


This function is used to create the parser object that will be used by the rest of the process. This object is used to store data and configuration options and is passed to each of the functions involved.

$xml_parser = xml_parser_create();


Next we need to set up the functions that will be used in the parsing of the script. The xml_set_handler() method takes the following parameters:

PHP Logo

Round Timestamp To Nearest Time With PHP

19th March 2010 - 1 minute read time

If you want to create a "rounded" time stamp, for example, to the nearest 15 minutes use this as a reference:

function roundTime($amount, $units = 'm', $time = null)
    if (is_null($time)) {
        $time = time();
    if ($amount == 0) {
        $amount = 1;
    switch ($units){ 
        case 'm':        
            $roundBy = 60 * $amount;
        case 'h':
            $time = $time + 60 * 60 * $amount;        
            $roundBy = 60 * 60 * $amount;
        case 'd':
            $time = $time + 60 * 60 * 24 * $amount;
            $roundBy = 60 * 60 * 24 * $amount;

    return round($time / $roundBy) * $roundBy;

echo "\n";
echo date('r', roundTime(15, 'm'));
echo "\n";
echo date('r', roundTime(2, 'h'));
echo "\n";
echo date('r', roundTime(3, 'd'));
echo "\n";
echo date('r', roundTime(-3, 'd'));
echo "\n";
PHP Logo

PHP IP To Location

1st March 2010 - 12 minutes read time

Converting an IP address into some useful location information can be useful if you want to find out where sites are hosted or customise content to users depending on their location.

All this code is freely available over at github.

There are several ways to do this, all of which have their advantages and disadvantages, but sticking with one can cause rewriting a lot of code in the future. So rather than pick one and stick with it I decided to use dependency injection to allow different classes to be used that convert IP addresses to locations in different ways. The first task is to create an abstract class that will be used to construct the rest of the IP location classes. Each class that extends this abstract class will contain a method called getIpLocation() that will convert an IP address into a location, and a method that will update the data source for the location lookup. Rather than lump all of the classes into a single directory I have created a directory called Service, into which all of the different classes that lookup IP addresses will be kept.