Monday, March 5, 2012

Detecting user's timezone

There are two methods that can be used to detect user's timezone:

1. BY using JavaScript to get clients timezone and send it to php

2. By using geolocation data and IP address of the users PC.

List of Supported TimeZones

Link1

Link2

Link3

js detect

Link4

geolocation tool

php web service

Sunday, March 4, 2012

Friday, March 2, 2012

Time and Date related PHP functions

Overview

In this post I will explain how can you use time and date related php functions in order to manipulate current time of the user that is using your web app no matter where he is in the world.

Before I start all time/date related functions in php work accordingly to the timezone property in the php.ini file


date.timezone = "continent/city"


For example I am in Europe and the city of Skopje. So I edit my time zone with this value

date.timezone = "Europe/Skopje"


The default is I think date.timezone = "Europe/London". But anyway if you are developing multilingual application or it will be used from all over the world you should adjust your time zone in php.ini or use the default with adjusting the time in you code.

There is a function called

date_default_timezone_set() 

Here date-default-timezone-set, which can be used in you php code to change the timezone of the running script at runtime. So for example if in the php.ini is set to date.timezone = "Europe/Skopje" if I create a script test.php with this code 

[php tag]

date_default_timezone_set('Europe/London');
echo "date = ".date("D M j G:i:s T Y");

[/php tag]

Output: date = Fri Mar 2 10:29:33 GMT 2012

it will return that I am one hour behind. Thats because I restore the timezone to the default value i.e London

Note: As far as I was able to test this function, it doesnt work with just value "Skopje", so it must be used parameter like this "Europe/Skopje". Maybe there are also other misses like this but I dont know them at the moment.


Note: You can also use date_default_timezone_get() which will return the current timezone that is set by php.ini or overrided if date_default_timezone_set is used.


Basic Functions

1. time() gives the current time in seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
That means I can always know the current time and the number returned, can be used to convert to current date and time.

 I used two functions to get the current time according to php.ini timezone: date(), and localtime().

2. date() accepts two parameters $format and $timestap == time() function.
So it is neccessary to provide the format of the output. The timestamp defaults to the time() function.
I use this format for my local date "D M j G:i:s T Y".

D = day, M = month, j = day of the month, G = 24 hour format, i = minutes with leading zeroes
s = seconds with leading zeroes, T = Timezone abbreviation, Y = year.

There are more parameters you can add and also you can change the order of each.

3. localtime() is a function that also accepts two parameters, timestamp and type of the array returned, true if associative and false if numerically indexed.

So I use this code snippet to get formatted array

echo "<pre/>";
print_r( localtime(time(),true));
output:

Array
(
    [tm_sec] => 3
    [tm_min] => 3
    [tm_hour] => 12
    [tm_mday] => 2
    [tm_mon] => 2
    [tm_year] => 112
    [tm_wday] => 5
    [tm_yday] => 61
    [tm_isdst] => 0
)

it returnes array of values.

tm_sec = seconds,
tm_min = minutes,
tm_hour = hour 0-23,
tm_mday = month 1-31,
tm_mon = month 0-11,
tm_year = years since 1900,
tm_wday = day of the week 0-6,
tm_yday = day of the year 0-365,
tm_isdst = is daylight savings time in effect? Positive if yes, 0 if not, negative if unknown


I found this webpage on wikipedia which was very useful for me http://en.wikipedia.org/wiki/GMT
So we measure in UTC time format which is equvalent to GMT. Just to mention that meridian time is not always the same as the time we use on daily basis. This means that for example in Iceland meridian time would be UTC-01 but they use the UTC+00 because of more comfortable usage and other reasons (Internet, etc.)
 
     

Wednesday, February 29, 2012

Wednesday, February 15, 2012

Memcached Overview

 General

What is memcached:  "Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load." You may want to visit: Memcahed Wiki.

Memcached is developed in C programming language so that makes it cross platform and many other languages  have API interfaces to it so it can be used with lot of other technologies.

Installation

Installation on linux is a breeze :)  just run this command in terminal

- sudo apt-get install memcached

I got to a problem when installing memcached with a missing library pcre.h 
Well run this command in terminal

- sudo apt-get install libpcre3-dev


And I found this blog with the answer for this at pcre.h missing
Memcached has some default settings which are best for most server configurations so select all defaults when installing. If you have some special requirements visit the conf page .

If you want to use Memcached with PHP you will need PECL extension called memcache which is not included by default with PHP package, so you have install it manually. Open terminal and run this command

- sudo pecl install memcache

Accept all defaults. Once it is built and installed you may need to add this line to your php.ini file

- extension=memcache.so

into you extension section.

Well thats it for the installation, you are ready for caching :)

 Tutorial

When starting up with Memcached I recomend reading of the Caching story :) and also to illustraded one :)

So the basic idea of memcached is to store all your server adresses in an array like this

$MEMCACHE_SERVERS = array(
   "10.1.1.1", //web1
   "10.1.1.2", //web2
   "10.1.1.3", //web3
);

and put them into a memcache object like this

$memcache = new Memcache();
    foreach($MEMCACHE_SERVERS as $server){
    $memcache->addServer ( $server );
}


The next thing you should do is to use memcached i.e utilize the caching systems on all your servers in the cluster. In this example we have 3 with one 1 GB RAM, so that means 3 GB of distributed cache.

 Illustrations

Below are some images I made about memcahed usage and structure







Wednesday, February 8, 2012

PHP Accelerators Overview

When you have a dynamic web application that has a need of big payload of data at once you will need a caching management system that will optimize the loading process by scheduling what, how much and when to be loaded. Of course there are several PHP extensions that are available. But first to mention is that there are two levels of optimizing the loading of the data in a web appication:

level 1: local optimization
level 2: distributed (over several application servers)

maybe level 3: (disk caching if plausible)

level 1: For the first level now there are several well known accelerators like APC(Alternative PHP Cache), eAccelerator, XCache. Here is a wikipedia page for them opcode php cachers. The most popular is APC which will be built into the next versions of the php, probalbly 5.5 or newer.  APC and alike are very useful for bytecode caching i.e when you have many php pages to load it would be better to have them cached in memory rather than load them with each php request. But in either case you can use also data caching. When the data is distributed you have to use something else.

level 2: The second level is for web applications that are distributed accross several web application servers and have need of distributed data. Here comes into place one and well know caching system called memcached (http://memcached.org/)