PHP Strange Timezone problem

On my system Timezone is Europe/Berlin in php.ini

//Block 1
$z = \'2009-10-25\';
$a = strtotime($z);
$b = date(\'r\',$a); //Format date from int time stamp to string
echo $b;
//value of $b will be Sun, 25 Oct 2009 00:00:00 +0200

//Block 2
$z = \'2009-10-26\';
$a = strtotime($z);
$b = date(\'r\',$a); //Format date from int time stamp to string
echo $b;
//value of $b will be Mon, 26 Oct 2009 00:00:00 +0100

1. When date is 25th Oct timezone is +2
2. When date is 26th Oct timezone is +1

You can see the difference in timezone caused by strtotime.
Assuming from 26th Oct, winter considered to be start & DST will be applied
in central Europe.

Now is is confirmed that, problem is caused by timezone setting change or DST.

If I keep timezone to UTC, then I can not see this problem. To overcome this issue I set timezone to UTC in my function. This behaviour seriously affects my complex program which calcultes hours.

Please let me know if you have any DST related official information. I mean when it will apply on which date.

Clock changes in Berlin


Gmail/Gdata Contacts & Group via Proxy

In last few days I got chance to see Gdata API. This is nice. I tried to retrieve Gmail Contacts via Proxy server in PHP code using Zend Gdata libriary. Actually Zend Gdata libriary do not have Contacts object like it has for other Google service like calendar, Youtube etc… But Google contacts can be accessed via Zend_Gdata classes easily on direct internet connection but It has some problem with Proxy connection. It can be solved by providing proxy connection parameters while creating Zend_Http_Client object like following:

$httpProxy = array(
       'adapter'      => 'Zend_Http_Client_Adapter_Proxy',
       'proxy_host'   => '',
       'proxy_port'   => 3128
$clientProxy = new Zend_Http_Client('', $httpProxy); 

//Client authentication
$client = Zend_Gdata_ClientLogin::getHttpClient($email, $passwd, 'cp', $clientProxy, 'My-ContactFetcher-0.1'); //cp for contacts

Above code help to do some client authentication via proxy server. But If we use $client object make request to retrieve feed data then it gives some errors saying ‘Can not connect to tcp://’. Now it seems we have already provided http proxy detail to $clientProxy object, why it is giving error. But http proxy detail needs to provide again before making request to retrieve feed data. Code is like following


Above $client object can be used to call following functions.

Google contacts can have group so to retrieve associated group of contact. First we need to retrieve all groups & its ids. Because only id of group is stored with contact. Following is code to retrieve all groups & its ids.

function getAllContactsGroups($client,$mail)
       $scope= "".urlencode($mail)."/";
       $gdata = new Zend_Gdata($client); 
       $query = new Zend_Gdata_Query($scope.'full');
       $feed = $gdata->retrieveAllEntriesForFeed($gdata->getFeed($query));
       $arrGroups = array();
       foreach ($feed as $entry){
		$arrGroups[$entry->id->text] =  $entry->title->text;
       return $arrGroups;

Following is code to retrieve all contacts. This code also finds its group id, that is retrieved with contact entry. At the end it prints contact name, its email & its group name.

function getAllContacts($client, $email)
	$arrContactGroups = getAllContactsGroups($client,$email);
	$gdata = new Zend_Gdata($client);
	$query = new Zend_Gdata_Query('');
	$feed = $gdata->retrieveAllEntriesForFeed($gdata->getFeed($query));
	$i =1;
	$arrContacts =  array();
	foreach($feed as $entry){ 
	$name = $entry->title->text;
	$parts = $entry->getExtensionElements(); 
	$contactGroup = '';
		foreach($parts as $p){ 
			$element = $p->getDOM(); 
			case 'email': 
				$email = $element->getAttribute('address'); 
			case 'groupMembershipInfo': 
				$contactGroup = $element->getAttribute('href'); 
	echo $i++.") ".$name."=". $email ."=". $arrContactGroups[$contactGroup]."<br/>" ;	

Zend Framework Bootstrap

Bootstrap is a setup process of different framework component to make easily accessible/available in application. It is very important to setup all require component of framework, so whenever it can be used effectively without any hassle. Bootstrap file is very important file for project using Zend Framework. Bootstrap file or bootstrap process is not a standard in Zend Framework, but to create bootstrap file but it is good practice. Many people use it many way.

It is important to know what are the component of Zend Framework is require. Following is good example of bootstrap.php

require_once 'Zend/Loader.php';

class Bootstrap

    public static $frontController = null;

    public static $root = '';

    public static $registry = null;

    //Primary function will be called first from index.php, it will call rest of functions to boot
	public static function run()
        $response = self::$frontController->dispatch();

    /*It is used to prepare from macro view.
	It gives macro view idea about preparation.
	Sequence of each call is important.
	public static function prepare()

		//To load all classes automatically, without include or require statement


    //Error reporting setting
	public static function setupErrorReporting()
    	ini_set('display_errors', true);

    //Path settings will be done using this
	public static function setupPath()
        $root = dirname(dirname(__FILE__));
		    $root . '/library' . PATH_SEPARATOR .
			$root . '/application' . PATH_SEPARATOR .
			$root . '/application/models' . PATH_SEPARATOR .
        self::$root = dirname(dirname(__FILE__));

	//Date time setting will be done here
	public static function setupDateTime()

    //Registry setting will be done here.
	public static function setupRegistry()
        self::$registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS);

    //Configuration file reading & setting up configuration will be done using following.
	public static function setupConfiguration()
        $config = new Zend_Config_Ini(
            self::$root . '/application/config/config.ini',
        self::$registry->configuration = $config;

    //Important: Setting of front controller will done here.
	public static function setupFrontController()
        self::$frontController = Zend_Controller_Front::getInstance();
                'default' => self::$root . '/application/controllers',
				'admin' => self::$root . '/application/admin/controllers'
        self::$frontController->setParam('registry', self::$registry);

    /*View setup will be done here.
	Setup view encoding, layout setup also done here
	public static function setupView()
        $view = new Zend_View;
        $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
                'layoutPath' => self::$root . '/application/views/layouts',
                'layout' => 'common',
                'pluginClass' => 'ZFBlog_Layout_Controller_Plugin_Layout'

    /*DB setup done here.
	Read configuration, create db instance & set in registry to use in other part of application.*/
	public static function setupDatabase()
        $config = self::$registry->configuration;
        $db = Zend_Db::factory($config->db->adapter, $config->db->toArray());
        $db->query("SET NAMES 'utf8'");
        self::$registry->database = $db;

    //Response will be sent from following function, also set header for response.
	public static function sendResponse(Zend_Controller_Response_Http $response)
        $response->setHeader('Content-Type', 'text/html; charset=UTF-8', true);

	//Logger setup will can be done using following
	public static function setupLogger()
		* Create a log formatter
		$format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
		$formatter = new Zend_Log_Formatter_Simple($format);

		* Create a file logger
		$stream = @fopen(APP_DIR . "tmp/logs/" . date("Y-m-d") . ".php", 'a', false);
		if (!$stream)
			throw new Exception('Failed to open stream');
			$fileWriter = new Zend_Log_Writer_Stream($stream);
			$fileLogger = new Zend_Log($fileWriter);
			Zend_Registry::set("fileLogger", $fileLogger);
		catch (Zend_Log_Exception $e)
			echo "Error: " . $e->getMessage();
		catch (Zend_Exception $e)
			echo "Error: " . $e->getMessage();

	Zend session setting done here.
	public static function setupSession()
				'save_path' => APP_DIR . "/tmp/sessions",
				'remember_me_seconds' => 7200,
		catch (Zend_Session_Exception $e)
			$dbLogger->log('Error: ' . $e->getMessage(), 1);
			$fileLogger->log($e->getMessage(), 1);
		$defaultNs = new Zend_Session_Namespace('default');

		Zend_Registry::set("defaultNs", $defaultNs);

		if($config->log_level == 2)
			$fileLogger->info("Sessions setup finished!");


To run this bootstrap file we just need to add following code to index.php

require '../application/Bootstrap.php';

There are many Zend Framework component that is used in normal/regular/usual project but I have not placed it in above code listing. Please suggest.

Please feel free to provide your feedback.

PHPMailer BUG : Splits the subject line in the middle of a multi-byte character

PHPMailer is one of my favorite library for sending email since long. I though it is really mature library and still I believe it. Nowadays its getting more & more mature.  I was using PHPMailer v1.73.

Last week I week, I have seen major bug in my application. It makes junk char/mojibake in subject line. I use all content in utf-8. I set all settings to make email completely utf-8. Then I try to read code of my project 2-3 times but it has not problem at all. Then I moved to Google to search solution for the problem. I found following links.

#2957 ([PATCH] Long utf-8 encoded subject fails (phpmailer bug)) – symfony – Trac

Then I realized that problem is in PHPMailer library. Following is the actual problem. If a subject line contains non US ASCII characters and the encoding is set to UTF-8, phpMailer occasionally splits the subject line in the middle of a multi-byte character, causing the encoded representation to appear in the email client. Specially problem appears when email subject contains multi-byte char & length is more than 20 char. You can usually find problem around or after 20th char.

#2957: encoding.patch – symfony – Trac

The above link has also given solution to the problem. But before adopting patch,I visited the PHPMailer site ( I found PHPMailer 2.2.1 is already released on July 20 2008. Then I downloaded the latest version & checked its code, same patch is found in latest release. So I upgraded to PHPMailer 2.2.1.

8 Steps To Avoid Mojibake

Mojibake problem is common problem which is usually seen with many non English sites or program. Non English content needs some special treatment while processing, saving, showing or exporting. There are many mistake made during software development which creates mojibake problem. There are few precaution to take to avoid mojibake problem.

Following are steps to avoid mojibake

  1. Check encoding of program file. Many time file is not save in required encoding or corrupted. File itself contain some mojibake char. To avoid this problem use suitable editor which supports require encoding. While saving file check encoding/char set property with available program (Many editor provides “Character Code” or “Encoding” property in SAVE/SAVE AS dialog box).
  2. Verify following properties in database at different level (Server, Connection, DB, Table, Field)
    -Charset, Collation
  3. Set suitable charset or locale settings just after connecting to DB. This will solve some big problem.
    For example if you are using PHP-MySQL:

    /************** PHP Code starts *****************/
    //Connect to mysql
    mysql_connect($host, $uid, $pwd, true);
    //select database

    //Set connction charset
    @mysql_query(“SET CHARACTER SET $charset”);

    //Set connction charset
    mysql_query(“SET NAMES $charset;”);
    /************** PHP Code ends *******************/

  4. Set suitable character encoding in web page using meta tag. like following.
    <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
  5. Set suitable charset for CSS file & Javascript.
    Put something like following in first line of CSS file
    @charset “UTF-8”;
    Define charset property in <script> tag
  6. Set suitable value for following property for email, while sending email from program.
    CharSet, Encoding, ContentType
  7. While exporting data as CSV set suitable value for following header properties.
    Content-Type, Content-Disposition, Content-Language, Content-transfer-encoding
  8. While importing/exporting/converting content between different format, always check for charset/encoding property in available program.

Some terminologies need to know while dealing with i18n or l10n.


Character encoding:

Code page:

Windows code page:



Shift JIS: