Archive for March, 2011

PHP: Array as the arguments part of the sprintf function

Using sprintf() function is a great way to integrate variables into string templates. It produces nice and readable code. But the drawback is that sprintf() does not support array as the argument part. As result the code may look less readable as number of parameters increases.

In that case vsprintf() is your friend. It works exact the same way as sprintf() except it accepts array instead of separate arguments.

$link = sprintf('<a href="%s">%s</a>', $url, $title);
$params = array($url, $title);
$link = vsprintf('<a href="%s">%s</a>', $params);

A pointer within a pointer

A pointer within a pointer

If for any reason you have not seen Inception yet, you should definitely check it out.

PHP: Daylight Savings Time Support

The ‘old’ way to support time zones in PHP did not have built-in full time zones support. Each time date(‘I’) flag had to be checked to verify daylight savings time period.

For example, to convert local time to GMT could be done the following way.

// time zone difference in hours
$tz = -5;
 
// local date and time
$local = '2011-03-24 11:00:00';
 
// adjust time zone difference in hours
// using daylight savings time flag and convert to seconds
$diff = ($tz + date('I', strtotime($local))) * 3600;
 
// convert to GMT by subtracting $diff number of seconds
$gmt = date('Y-m-d H:i:s', strtotime($local) - $diff);
 
// the output is 2011-03-24 15:00:00
print $gmt;

However utilizing DateTime class included in PHP 5 makes time zone related operations less stressful.

$local = '2011-03-24 11:00:00';
 
// create and instance of DateTime
// using local date/time and its time zone
$date = new DateTime($local, new DateTimeZone('America/New_York'));
 
// change time zone
$date->setTimezone(new DateTimeZone('GMT'));
 
// the date is already converted to GMT
// so the output is 2011-03-24 15:00:00
print $date->format('Y-m-d H:i:s');

So using time zone name instead of actual difference in hours or seconds allows to take care of daylight savings time automatically.

However unlike MySQL CONVERT_TZ() PHP uses different list of time zone names. For example, for Eastern standard time PHP offers ‘America/New_York’ time zone name while MySQL prefers ‘US/Eastern’.

Kitten in Slow Motion

Zend Server JobQueue issue

Zend Server may be a really helpful product and only ZendJobQueue by itself is a great concept for multitasking, queuing or even regular cron replacement. But unfortunately it lacks a good documentation

For instance I have not managed to find anything official about ZendJobQueue as nearly good as Zend Framework documentation. There are some good blog posts and presentations though which were great to start with. But they do not include in-depth details or troubleshooting guide.

In my case I ran into the following error message: ‘Bad response from Job Queue server to a createHttpJob request. QLocalSocket: Remote closed.’ once I tried to go beyond simple test code.

Although Zend Server was up and running and test ZendJobQueue code was working, it looked like everything had crashed really bad…

A quick search showed that someone had already run into a similar Job Queue problem about a year ago. No solution has been posted though. But the fact that my test code still was working helped to address the issue.

So here is my original call which caused the error message showed above.

$params = array('paramValue');
 
$q = new ZendJobQueue();
$q->createHttpJob('/task.php', $params);

And this is the call which works perfectly

$params = array('paramName' => 'paramValue');
 
$q = new ZendJobQueue();
$q->createHttpJob('/task.php', $params);

So apparently the $params array must have keys!
Of course one can tell that simply by looking at the error message but it was not that obvious for the first hour.

P.S.: Zend Server 5.1 Development License.

FogBugz and Kiln World Tour Talk by Joel Spolsky

Joel Spolsky’s talk on the latest features in FogBugz 8 and Kiln 2.
The same awesome talk I was able to attended last October in ATL. He not only showed all great features and benefits of FogBugz and Kiln but made fun of traditional slides and presentations.