Raza Mehdi's Blog

Archive for June, 2010

Working with Time Zones in PHP

by on Jun.19, 2010, under PHP

A few days ago  at work, one of my colleagues had a task to display three digital clock in PHP for Barbados, Los Angeles and London. He asked for my help, and i kind of started working on how to do this. I obviously chose jquery to handle all the JS stuff for me. Here is what i did:

First for getting the timezones for specified locations, i created a PHP file ‘setTimes.php’, containing the following code:

$timezones = DateTimeZone::listAbbreviations();

/*
// Using This Code i found out the timezones the specified locations are using ..

$tzkeys = array_keys($timezones);
foreach($tzkeys as $tzk) {
$tzone = $timezones[$tzk];

for($i=0;$i<sizeof($tzone);$i++) {
  $tzitem = $tzone[$i];
  foreach($tzitem as $tzi)
    echo "$tzi<br />";
    echo "<p></p>";
  }
}
*/

$londontime = $timezones['bdst'][0];
$usptime = $timezones['pdt'][0];
$barbtime = $timezones['bmt'][0];

$tzid = array($londontime["timezone_id"],$usptime["timezone_id"],$barbtime["timezone_id"]);

$timevalues = "";

foreach($tzid as $tz) {
date_default_timezone_set($tz);
$timevalues.= date("h:i:s A").'___';
}

echo json_encode(array($timevalues));

Now lets look at the code line by line:

$timezones = DateTimeZone::listAbbreviations();

This will store all the PHP supported timezone information in an array… Now look at the commented code and find out your specific timezone valus. I simply searched for the relevant timezones and stored their ids in the code below:

$londontime = $timezones['bdst'][0];
$usptime = $timezones['pdt'][0];
$barbtime = $timezones['bmt'][0];

Then i simply store the above values in an array, and then iterate through each value to get the current time in that timezone. After i simply encode the values in JSON using PHP’s native json_encode

$tzid = array($londontime["timezone_id"],$usptime["timezone_id"],$barbtime["timezone_id"]);

$timevalues = "";

foreach($tzid as $tz) {
date_default_timezone_set($tz);
$timevalues.= date("h:i:s A").'___';
}

echo json_encode(array($timevalues));

Now create a file index.php and add the following code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Digital Clocks in PHP</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>

<body onload="startclocks()">
<p>London Time: <span id="londontime"></span></p>
<p>US Pacific Time: <span id="usptime"></span></p>
<p>Barbados Time: <span id="barbtime"></span></p>

<a href="#" onclick="stopclocks()">Stop All Clocks</a>
</body>
</html>

In the script.js, enter the following code:

var clockvalues = null;
var clocksrunning = false;

function stopclocks() {
	if(clocksrunning) {
		clearTimeout(clockvalues);
		clocksrunning = false;
	}
}

function showclocks() {

	$.ajax({
	  url: '/tztest/setTimes.php',
	  success: function(data) {
		var timevals = data.split("___");
		timevals[0] = timevals[0].substr(2);
		//alert(timevals[0]+" "+timevals[1]+" "+timevals[2]);

		$("#londontime").html(timevals[0]);
		$("#usptime").html(timevals[1]);
		$("#barbtime").html(timevals[2]);
	  }
	});

	clockvalues = setTimeout("showclocks()",1000);
	clocksrunning = true;
}

function startclocks() {
	stopclocks();
	showclocks();
}

I think the code itself is self-explanatory. So no need to explain that i believe … :) . Dont forget to download the Sample Code too. Happy coding ..

1 Comment more...

Adding Custom Decorators to Zend_Form in Zend Framework – Part 1

by on Jun.14, 2010, under Zend Framework

Hello Everyone!

I just started work on my Real Estate Management System (REMS). Its first form is a booking search form for properties available for rent. The form consisted of fields like location, start & end booking dates,  no.of bedrooms, price range etc. Typically any element defined inside a Zend_Form class is wrapped like this on a view file:

<dl id="ELEMENTID">ELEMENT LABEL</dl>
<dd>ELEMENT</dd>

I wanted to get rid of this default styling, so after reading the documentation i achieved it by writing this code in the init() function of my Zend_Form class after defining all the form elements:

$this->setElementDecorators(array('ViewHelper'));

I also converted my Login Form’s layout to a table-based also. Here is the code for that class:

<?php

class Form_Login extends Zend_Form {

public function init() {

$username = $this->createElement('text','username');
$username->setLabel('Username');
$username->setRequired(true);
$username->setAttrib('size','20');
$this->addElement($username);

$password = $this->createElement('password','password');
$password->setLabel('Password');
$password->setRequired(true);
$password->setAttrib('size','20');
$this->addElement($password);

$submit = $this->createElement('submit','submit',array('label'=>'Login'));
$this->addElement($submit);

// Code for Custom Decorators here ...
$username->setDecorators(array(
'ViewHelper',
'Errors',
array(array('data'=>'HtmlTag'), array('tag' =>'td','class' =>'element')),
array('Label',array('tag'=>'td')),
array(array('row'=>'HtmlTag'), array('tag'=>'tr')),
));

$password->setDecorators(array(
'ViewHelper',
'Errors',
array(array('data'=>'HtmlTag'), array('tag'=>'td', 'class'=>'element')),
array('Label', array('tag'=>'td')),
array(array('row'=>'HtmlTag'), array('tag'=>'tr')),
));

$submit->setDecorators(array(
'ViewHelper',
array(array('data'=>'HtmlTag'),array('tag'=>'td','class'=>'element')),
array('Label',array('tag'=>'td')),
array(array('row'=>'HtmlTag'),array('tag'=>'tr')),
));

$this->setDecorators(array(
'FormElements',
array('HtmlTag',array('tag'=>'table')),
'Form',
));

}

}

?>

I hope this helps everyone out. Also checkout this great article Decorators with Zend_Form on Zend DEVZONE.

Leave a Comment more...

Get a Client’s Remote IP Address in PHP

by on Jun.09, 2010, under PHP

In PHP, we use $_SERVER superglobals to access different server parameter values. We can also get the remote client IP address by use of this superglobal. But what if the remote client is connected to network at his/her location, and is accessing the site. It means there is a proxy through which he access the internet. To overcome this problem, write the following code in your PHP script to get a client remote IP address:

<?php

if (getenv(HTTP_X_FORWARDED_FOR)) {
$ipaddress = getenv(HTTP_X_FORWARDED_FOR);
} else {
$ipaddress = getenv(REMOTE_ADDR);
}

?>

Leave a Comment more...

Diving into Zend Framework

by on Jun.09, 2010, under Uncategorized

Hello everyone!

Just started development on a real estate management system. Built on top of Zend Framework, i intend to use JQuery and Doctrine to handle the database queries. I am currently working on the requirements and the database design. I will further post an update on it as soon as possible.

REGARDS,

Raza Mehdi

Leave a Comment more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!