First and last day of the month in PHP

I’ve been working with dates today and i’ve found a pretty neat snippet to show the first and the last day of the month using php

		$firstDoM = date("Y-m-01"); //01 -> first day of month
		$lastDoM = date("Y-m-t"); //t -> last day of month

Remove MS Word formatting using PHP regular expressions

Recently i’ve had a client which would only paste into the CMS text from MS Word. Normally a JS should have been in place to automatically strip the crazy tags Word adds, but on this project this was not the case. Here’s hos to remove MS Word formatting using PHP regular expressions:

// it will remove all  comments
preg_replace('//i', '', $string);


// it will remove all  comments
preg_replace('//i', '', $string);
PHP zend framework

Zend Framework disable layout and view rendering

When using Zend Framework, sometimes you need to create a controller action which just does something, doesn’t need to display anything to the user. To disable the layout and the view rendering add the following in your action:

		// disable layout
		$this -> _helper -> layout() -> disableLayout();

		// disable view rendering
		$this -> _helper -> viewRenderer -> setNoRender();
zend framework

Zend_Auth and subdomains

I was working today on an user authentication system that redirects the users to their own sub-domain after a successful login. It should work like this: user comes to, fills in his credentials and after successful login he/she should be redirected to All fine and dandy except the session is not persistent between sub-domains of After a bit of google and some help from eXcuvator on #zftalk, i found the solution:

Just put the following in your Bootstrap.php and everything should just work:

    protected function _initSession()

        Zend_Session::setOptions( array('cookie_domain' => '', 'use_only_cookies' => true, 'name' => 'yourSessionName'));

First day timestamp of week number in PHP

function mondayTimestamp($weekNumber, $year)
$time = strtotime($year . '0104 +' . ($weekNumber - 1) . ' weeks');
// Get the time of the first day of the week
$mondayTime = strtotime('-' . (date('w', $time) - 1) . ' days', $time);

return $mondayTime;

Test it as follows:

$mondayTimestamp = mondayTimestamp(46, 2010);

echo date('j F Y', $modayTimestamp);

Fix php base64_decode error

If the base64_decode() returns weird results, you might try to decode a string which is not base64 valid. The most common problem seems to be + being replaced with a space (” “) when sending the encoded string via $_POST or $_GET.
To fix that simply do a str_replace on the encoded string, then try to decode it:

$encodedMessages = str_replace(' ', '+', $encodedMessages);
echo base64_decode($encodedMessages);

Hope this will help someone, it took me several hours to figure it out.

linux PHP ubuntu xdebug zend server

Setting up Xdebug with Zend Server on Linux

Since our team work on different operating system, we switched for custom apache/php/mysql installs to Zend Server. Before everyone was using something different (the guys on windows were using XAMP, WAMP, on ubuntu i was using the packages in the repo and on the server we’re using apache from cPanel) and applications were behaving a differently according to the default settings for all those platforms.

So we decided to install Zend Server CE as it has more or less the same settings over different platforms. I’ve had some bad experiences installing ZSCE on a system that already had apache installed via apt, but after a clear install of Karmic, ZF was running like a charm. The install process described in the online documentation works great.

After install is done, make sure to symlink php, pecl, pear and phpize so you can access them system wide:

sudo ln -s /usr/local/zend/bin/ /usr/sbin/zendctl
sudo ln -s /usr/local/zend/bin/pear /usr/sbin/pear
sudo ln -s /usr/local/zend/bin/pecl /usr/sbin/pecl
sudo ln -s /usr/local/zend/bin/php /usr/sbin/php
sudo ln -s /usr/local/zend/bin/phpize /usr/sbin/phpize

At this point you should be able to run php -i in terminal and the phpinfo will be displayed.

Next step is to install xdebug via pecl by running:

sudo pecl install xdebug

If all went well you should have the xdebug library located at /usr/local/zend/lib/php_extensions/ . If you don’t have it there, then something went wrong and you should NOT continue reading. This issue must be sorted first.

Next you need to comment out the 1st line of /usr/local/zend/etc/conf.d/debugger.ini so it looks like this:

#open editor
gksu gedit /usr/local/zend/etc/conf.d/debugger.ini

# this is how the 1st two lines should look afterwards
; register the extension to be loaded by Zend Extension Manager

Xdebug needs to be loaded before Zend Extension Manager, that’s why you need to add the following line just on top of the /usr/local/zend/etc/ext.d/extension_manager.ini

#open the editor
gksu gedit /usr/local/zend/etc/ext.d/extension_manager.ini

#add this line on 1st line: 

#save the file

Restart zend server by running:

sudo /etc/init.d/zend-server restart

You can check if xdebug is working like so:

php -i |grep xdebug

The output should be something similar to this:

xdebug support => enabled
xdebug.auto_trace => Off => Off
xdebug.collect_includes => On => On
xdebug.collect_params => 0 => 0
xdebug.collect_return => Off => Off
xdebug.collect_vars => Off => Off
xdebug.default_enable => On => On
xdebug.dump.COOKIE => no value => no value
xdebug.dump.ENV => no value => no value
xdebug.dump.FILES => no value => no value
xdebug.dump.GET => no value => no value
xdebug.dump.POST => no value => no value
xdebug.dump.REQUEST => no value => no value
xdebug.dump.SERVER => no value => no value
xdebug.dump.SESSION => no value => no value
xdebug.dump_globals => On => On
xdebug.dump_once => On => On
xdebug.dump_undefined => Off => Off
xdebug.extended_info => On => On
xdebug.idekey => mimir => no value
xdebug.manual_url => =>
xdebug.max_nesting_level => 100 => 100
xdebug.profiler_aggregate => Off => Off
xdebug.profiler_append => Off => Off
xdebug.profiler_enable => Off => Off
xdebug.profiler_enable_trigger => Off => Off
xdebug.profiler_output_dir => /tmp => /tmp
xdebug.profiler_output_name => cachegrind.out.%p => cachegrind.out.%p
xdebug.remote_autostart => Off => Off
xdebug.remote_enable => Off => Off
xdebug.remote_handler => dbgp => dbgp
xdebug.remote_host => localhost => localhost
xdebug.remote_log => no value => no value
xdebug.remote_mode => req => req
xdebug.remote_port => 9000 => 9000
xdebug.show_exception_trace => Off => Off
xdebug.show_local_vars => Off => Off
xdebug.show_mem_delta => Off => Off
xdebug.trace_format => 0 => 0
xdebug.trace_options => 0 => 0
xdebug.trace_output_dir => /tmp => /tmp
xdebug.trace_output_name => trace.%c => trace.%c
xdebug.var_display_max_children => 128 => 128
xdebug.var_display_max_data => 512 => 512
xdebug.var_display_max_depth => 3 => 3

Note the xdebug support => enabled on line 2.

Hope that helps 🙂

PHP zend framework

How to update your Twitter status using Zend Framework

After reading the NetTuts tutorial on how to update your Twitter status using CodeIgniter, I wanted to show how to update your Twitter status using Zend Framework.

For this tutorial you need to install Zend Framework and Zend_Tool first.

Step 1: setup apache vhost by creating the /etc/apache2/sites-available/twitter as follows:

    ServerAdmin [email protected]
    DocumentRoot /home/mimir/Zend/workspaces/DefaultWorkspace7/twitter
    ErrorLog /home/mimir/Zend/workspaces/DefaultWorkspace7/logs/twitter_error_log
    CustomLog /home/mimir/Zend/workspaces/DefaultWorkspace7/logs/twitter_access_log combined

Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all

You will need to change the DocumentRoot and Directory directives to match your configuration.

Step 2: add to your hosts file:

Step 3: create the Zend Framework project

# i switch to my Zend Studio workspace, where apache is reading his sites from:
# cd Zend/workspaces/DefaultWorkspace7/
zf create project twitter

Step 4: restart apache

sudo /etc/init.d/apache2 restart

At this point you should have a default Zend Framework project that you can browse at How let’s change our status 🙂

Replace the content of IndexController with the following:

setLabel('New Twitter Status')

        $submit = new Zend_Form_Element_Submit('submit');

        $updateForm->addElements(array($status, $submit));

        // we send the form to the view
        $this->view->updateForm = $updateForm;

        // we check if there was any POST
        if ($this->getRequest()->isPost()){
            $formData = $this->_request->getPost();
            // checking if the form data is valid (if we have a new status or not)
            if ($this->view->updateForm->isValid($formData)){
                // our form is valid, we can update our status
                $twitterStatus = $formData['status'];

                $twitter = new Zend_Service_Twitter('myusername', 'mypassword');
                $response = $twitter->status->update($twitterStatus);



Also replace the view for the index action of IndexController with:

Welcome to the Twitter Update Tutorial


TADA! You can now check your twitter account and see that twitter status is updated every time you submit the form -:)

PHP ubuntu zend framework

Install Zend Framework and Zend_Tool using PEAR on Ubuntu

I work every day with Zend Framework and the easiest way i find to install it is by using the PEAR packages offered by the betta channel ZF Campus

Here is what you have to do install Zend Framework and Zend_Tool using PEAR on Ubuntu:

sudo pear channel-discover
sudo pear install zfcampus/zf
# for some reason the installer does not correctly link the zf.php and Zend_Tool won't work
# so we need to setup a symlink
sudo ln -s /usr/share/php/zf.php /usr/bin/zf.php

That’s it! You can now test your install:

zf show version

In my care the output was this one:

[email protected]:~$ zf show version
Zend Framework Version: 1.9.2
eclipse PHP

Eclipse plugins written in PHP?

On another Eclipse related news, i have noticed today the GSOC 2009 ideas for eclipse. Although all the ideas are interesting, what catched my view was “Write Eclipse plug-ins in PHP
” through e4.

e4 is the community effort for building the next generation of the Eclipse Platform. The project has three principle aims:

  • Simplify the Eclipse programming model: This will
    reduce development and maintenance costs and enable a larger developer
    community to leverage the platform in their own Eclipse-based
  • Enable the platform for use on emerging web-based runtime technologies:
    This will ensure the platform remains a compelling and viable
    application framework in a rapidly changing web technology landscape,
    and it will allow e4-based applications to leverage web technologies,
    while remaining insulated from specific technology choices that may
    quickly become obsolete.
  • Broaden participation in development of the platform:
    This reduces the risks associated with building on a platform largely
    funded from a single source. Having a large and diverse group of
    commercial backers, will ensure the platform remains vibrant and viable
    over the long term.

Please refer to the project proposal for more information, including the initial list of committers. The e4 project summary page is here.

I’m really interested to see what comes out of that.