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.)