Category: php

PHP’s DateTime built-in class vs. Zend_Date

If you ever wanted a reason not to use PHP’s built-in DateTime class or to lie awake wondering about your code that uses it, here’s a good one. I’ve not looked into why this happens but 31st October minus one month gives 1st October. It should give 30th September. Zend_Date works in this respect:

Here’s a session showing this in action:

bob@desktop:~$ php -a
Interactive shell

php > $m = new DateTime();
php > echo $m->format( 'r' );
Mon, 31 Oct 2011 09:39:12 +1300
php > echo $m->modify('-1 month')->format( 'r' );
Sat, 01 Oct 2011 09:39:12 +1300

*cries*

bob@desktop:~$ php -a
Interactive shell

php > $m = new Zend_Date();
php > echo $m;
Oct 31, 2011 9:47:50 AM
php > echo $m->sub( 1, Zend_Date::MONTH );
Sep 30, 2011 9:48:25 AM

*correct*

 

Benchmarking PHP – a hands on solution

Screenshot of some PHP codeSometime you will be in a situation where benchmarking is to solve a particular performance problem. You can imagine how it goes – the system performs well on an unloaded development server with a few hundred records, but once in the wild on a production server with tens of thousands of records all of a sudden you’ve got an issue.

Finding bottlenecks in code isn’t always an easy task and it’s helpful to have some way of measuring the time it takes to run a process accurately. Sure, there’s the stopwatch method but this only scales so far. Accurately benchmarking is also great for measuring performance of code where the speed isn’t currently an issue. You can benchmark a known operation, then repeat this benchmark process in the future to ensure that no unintentional degradation of performance has occurred as a result of development.

Here’s an example piece of code to help you quickly benchmark code. It supports loading multiple tests into it and simply does a var_dump of the results. Using these results in creative ways is an exercise for the reader.

<?php
 
    class Benchmark
    {
        private $results;
 
        public function __construct()
        {
            // Our benchmarking may take some time - disable the time limit
            set_time_limit( 0 );
        }
 
        private function benchmark( $test, $callback )
        {
            $start = microtime( true );
            $callback();
            $this->results[ $test ] = microtime( true ) - $start;
        }
 
        public function runTests()
        {
            // -- Test #1 ---------------------------------------
 
            $this->benchmark( 'Fetch all contacts', function() {
 
                TwSCM::model( 'broadcast/contact' )->fetchAll();
 
            } );
 
            // -- Test #2 ---------------------------------------
 
            $this->benchmark( 'Fetch objects for tag 4', function() {
 
                TwSCM::model( 'tag' )->getObjectsForTag( 4 );
 
            } );
 
            // -- Test #3 ---------------------------------------
 
            $this->benchmark( 'Count to one million', function() {
 
                for( $a = 0; $a <= 1000000; $a++ )
                {
                    // Do nothing ...
                }
 
            } );
 
            // -- Output the results ----------------------------
 
            var_dump( $this->results );    
        }
    }
 
    $benchmark = new Benchmark();
    $benchmark->runTests();
 
?>

The above code requires at least PHP 5.3 (it uses closures to run the tests and time/store the results) and currently some tests that I’ve been running to profile Turboweb’s Simple Web Manager application. The benchmark() method takes two parameters – the first is the name of the test (this is displayed when $this->results is var_dump()ed) and the second is an anonymous function containing the test to be run.

By modifying the contents of the anonymous function and running this script multiple times you will be able to test and tweak your functions to as quickly as they can.

Good hunting!

FogBugz “Active Project” notification tool

At Turboweb we’ve recently subscribed to a 5-user on-demand license for an awesome case tracking system called FogBugz.  For us it means that we have controlled workflow of cases (bugs, features, enquiries etc.) along with awesome estimation reporting and time tracking.  FogBugz was built with development teams in mind and it’s a very nice tool.

Anyway, one of the things it has is a nice API which you can use to interrogate the product (read and write) and this got me thinking about how I could use the API to remind me what I should currently be working on.  Thus was created the “FogBugz Notification Tool”.

This script does the following:

  1. Connects to your FogBugz installation
  2. Runs a query to see what things have been worked on today
  3. If there are any items that don’t have an end date, then it’s the case that’s currently being worked on
  4. Get the details of the case
  5. Use notify-send to pop up a notification bubble showing case number, title, estimated time and time remaining.

FogBugz Active Case Notification

I have found this most effective if I put it in my crontab to run every 5 minutes.

Requirements:

  • Ubuntu Linux (or any version of Linux that supports the “notify-send” command)
  • PHP5 CLI (simply because this script is written with PHP)
  • The “libnotify-bin” package (this provides the “notify-send” command, try “sudo apt-get install libnotify-bin”)

The script requires a little configuration for your circumstances, but this is easily done by editing the defined constants in the script:

	// Adjust these defines to suit your installation
	define( 'FBBASEURL', 'https://mywebsite.fogbugz.com/' );
	define( 'FBUSERNAME', 'my@email.address.com' );
	define( 'FBPASSWORD', 'mypassword' );

And to add it to your crontab, simply add this line (note the */5 means every time the current number of minutes in the hour is evenly divisible by 5)

*/5 * * * * DISPLAY=:0.0 /usr/bin/php /home/bob/fogbugz-notify.php

Of course, adjust the path appropriately.

CodeIgniter – Form Validation and Optional Fields

I’ve been doing a bit of work lately with CodeIgniter.  It’s been OK but the framework seems quite wrong in most places.  My main gripe is that the “helpers” define themselves as functions in the global namespace (rather than as objects that hook into the CodeIgniter object) which results in some stupidity like a function called “set_value” which just happens to retrieve the value as validated by the form validation “helper”.  Anyway, that’s a lesson learnt.

I’ve been trying to get the form validation to allow me to use set_value for fields that aren’t required, but unless your rules specify the field and a validation rule for it the value won’t be available when set_value is called.  The trick here is to use “echo” as the rule which means that the validation passes and you get back what you put in.  This is not documented in the CodeIgniter manual (it’s implied by the fact that you can use any PHP function as a “rule”).

Example:

$rules = array(
  array(
    'field' => 'name',
    'label' => 'Name',
    'rules' => 'required'
  ),
  array(
    'field' => 'nickname',
    'label' => 'Nickname',
    'rules' => 'echo'
  )
);
 
$this->form_validation->set_rules( $rules );

In this case any calls to set_value(‘nickname’) will return the correct value, rather than an empty string.

PHP tutorial – introduction to classes

You might have noticed that I’ve migrated this blog to WordPress – the main reason for doing so was to give me the ability to easily add pages to the blog so that I could keep my web dev tutorials in one place.

I’ve also just added the introduction to classes tutorial so go and check it out.

WordPress Themes