Math,Dates and PHP (cont’d)

20 06 2010

The last posting considered the following snippet:

$d1 = “2005-01-01”;
$d2 = “2006-01-01”;
echo dateDiff($d1, $d2);

The question posed by a perspective employer was for me to write a dateDiff() that would indicate the difference in days between the dates represented in the hyphenated strings of variables $d1 from $d2. I was to accomplish this without using any of PHP’s built-in date/time features.

Initially, I ignored the constraints because the direction seemed senseless to me. So, yes, I used PHP built-ins like strtotime() and mktime(), too. Oh, my! I even got fancy and used gregoriantojd(). Remarks from perspective employer, however, indicated that only by taking an algorithmic approach would I stand a chance of gaining an in-person interview. So, I did the exercise, stuffing that code into a comment — a demonstration of doing things with PHP against the grain of the PHP Way. What is that way? Look first to see what built-ins PHP has and utilize them before “rolling your own.”

The algorithmic approach is supposed to show programming skill. But, I think programming skill especially as regards PHP also would necessitate that one know how to pick and choose.

So, let’s return to this question again and look at something else we might use from what already exists in PHP. If you’re going to work with dates/time in PHP, PHP5.3 has handy DateTime and DateInterval classes that can greatly assist us. Here’s how:


function dateDiff($d1,$d2)
{
$datetime1 = new DateTime($d1);
$datetime2 = new DateTime($d2);
$interval = $datetime1->diff($datetime2);
return "The number of days difference is: " . $interval->format('%a days');
}
$d1 = "2005-01-01";
$d2 = "2006-01-01";
echo dateDiff($d1, $d2);

Notice how much more natural this kind of coding style is. It is indicative of the way most people think by focusing on the difference between the first datetime object and the second one. You are not being forced to do or think about the math yourself, only to call the diff() method of the first object while passing in the second object as a parameter. This is easy to do and requires a few lines of code so it is an excellent and speedy alternative to what we’ve seen so far.

There’s just a couple of things to keep in mind. If you wish to use these classes you need PHP 5.3. Also, note the format specifier is using ‘a’. You need that if you want to see the total number of days. Lastly, I tried this code on Windows with Wampserver and even though I used PHP 5.3.1, I got an error due to a bug with PHP5.3 running on Windows. Apparently the bug is fixed if you’re using PHP 5.3.2 with Windows, or so it indicates at:
http://bugs.php.net/bug.php?id=51184&edit=1

When I ran the code with XAMPP using PHP5.3 on Mac OSX, the code ran fine and it also runs fine on Linux according to above url.

This work is licensed under a Creative Commons License
.

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: