pt_date: Work with dates in php

2011/03/13 Leave a comment

One of the most awful things when I work in PHP is to manage operations with dates. With the PHP 5 release came the announce of the new Date Class with operatios to add days,months, years, etc… to get the diff between to dates, and more and more. But when you try to use this ‘wished’ class, what happens ? At least for me frustation. How can the php team believe that the next code is ‘human readable’ or usable?

$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7Y5M4DT4H3M2S'));
echo $date->format('Y-m-d H:i:s') . "\n"; -> 2007-06-05 04:03:02

Oh my god, in my opinion this is the wrong way to help developers.

How I need to operate frequently with dates and I don’t want to use the native Date class, I have created my own class with clear function names and with chaining feature implemented.

Let me show some small examples:

- Add 2 months to the current date

date = new pt_date();

- Move to the begin of the month and add 5 hours:

date = new pt_date();

- Get days between 2 dates:

date1 = new pt_date("2009-10-15 00:00:00");
date2 = new pt_date("2009-10-22 00:00:00");
echo $date1->days_between($date2); --> 7


date1 = new pt_date("2009-10-15 00:00:00");
echo $date1->days_between("2009-10-22 00:00:00"); --> 7

I have published the code in github as a public open source project.

[ ]

I hope that you find it useful.

Categories: Project Tags: ,

pt_mock: Mock objects when testing in php

2011/01/16 Leave a comment

When I started with TDD in PHP, some years ago, I used the mock feature of PHPUnit but it disgust me a lot. At that time I worked at Domestika with Mathias Biilmann and Rodrigo Alvarez. All together we decided to create our mock class to work in a more simple way.

When I left Domestika I wrote, from the begining, my own version of that mock object with a similar behaviour of the original mock object but adding some new features.

Now I have finally published the code in github as a public open source project.

[ ]

Some examples of how to use it:

We have a method class that receives a object as parameter. Inside this method
we will call the object method ‘method_mock’ with parameter ‘a’
and we expect that it returns ‘b’.

    class class_a {

        function my_method($class_b) {
            return $class_b->method_mock(‘a’);


    $class_b = new pt_mock(‘Class B’);

    $class_a = new class_a();
    echo $class_a->my_method($class_b); // —-> ‘b’

To check that all expectations have been accomplished :

    $class_b->verify(); // for a instance of pt_mock
    pt_mock::verify_all(); // for all mocks instantiated

If you want to test that the method is called two times:


Sometimes you don’t want to test if a method is called, you only want that if a
method is called the mock object returns a value based on parameters.


    echo $class_b->method_mock(‘a’) —> ‘b’
    echo $class_b->method_mock(‘a’) —> ‘b’
    echo $class_b->method_mock(‘a’) —> ‘b’
    echo $class_b->method_mock(‘a’) —> ‘b’

And sometimes you want to raise a exception instead of to return data.

    $class_b->stubs(‘method_mock’)->with(‘a’)->raises(new Exception());

    echo $class_b->method_mock(‘a’) —> raises a exception

I hope that you find it useful.

Categories: Project Tags: ,

Invite your contacts with OpenInviter

2011/01/15 Leave a comment

Some days ago my boss ask me to give our users the option to invite their contacts to join to our website Citiservi. Googling around I finally found a great open source project called OpenInviter.

Now we can retrieve contacts from gmail, yahoo, hotmail, facebook, twitter… There are plugins for some email providers (61), CMS (27) and social networks (43). To be accurate currently there are 104 plugins ready to use. Seems to be good… and it’s good!

The requeriments are the next:
– PHP5
– curl or wget
– DOM extension

The installation is very quick, in five minutes openinviter is ready to start to be used.


$email = “”;
$passord = “my_password”;

$open_inviter = new OpenInviter();

if (!$open_inviter->getInternalError() &&
     $open_inviter->login($email, $password) &&
     $arr_contact = $open_inviter->getMyContacts()) {

     $oi_session_id = $open_inventer->plugin->getSessionID();
     foreach($arr_contact as $key => value) echo “{$key} : {$value}\n”;

In some cases, facebook as example, you can use the internal messaging system to send the invitation to user instead of to send an email manually.

$arr_message = array(‘subject’ => ‘Invitation subject’, ‘body’ => ‘Invitation body’);
$open_inviter->sendMessage($oi_session_id, $arr_message, $arr_contact);

You can update plugins in two ways: the autoupdate feature or manually. And install a new plugin is so easy as copy the plugin file into the plugins directory.

Categories: Uncategorized Tags: , ,