Sunday, January 22, 2012

A simple strategy for time localization

If you are building a webapp which is dependent on time (for example, a bidding or betting application) then you must have seen the requirement to show time on the webapp depending on the timezone of the logged in user. The other day i was going through various blogs and i saw a couple of posts by different blogs covering this approach and i was amazed to see that how complex route they took. So, i thought to write a post describing some really easy steps to achieve this.
There are multiple decisions/questions that you have to answer in order to achieve this goal.

1. In what timezone will you save the time in your db?
2. How would you get user's timezone?
3. When do you want to change the timezone according to user's timezone?

In this post i'll go through each of the above questions one by one.

In order to answer the first question, the best option is to go for GMT/UTC timezone. The reason is that it's extremely easy to convert time from GMT to GMT+8 or GMT0-5 etc. You can simply add/subtract the difference from the timestamp. But, you have to consider another thing here, if you are hosting your webapp on a shared server, then you need to consider the timestamp your shared hosting server is using. Because for any time critical webapp, you'll be frequently getting the current time and the server will be returning you the timestamp depending on the timezone it has set. Most of the shared hosting servers don't allow you to change the timezone. Now, you can also keep GMT/UTC timezone as the default timezone and you can change the timezone of the current time as well. Both are valid ways,  so it's up to you what to go for. Now, whenever user enters some datetime, you convert the datetime entered into the default timezone and save it in your db.

Now, the second question, i.e. how would you get user's timezone. Now there are a couple of ways to achieve this.

1. Ask the user for his timezone upon registration and use that
This is a fairly popular and accurate method of getting user's timestamp. The only problem in this approach is that it's not dynamic, i.e. if the user moves to a different timezone, he'll have to manually change his timezone on your webapp. If you don't mind putting your users through this step, then this method is recommended.

2. Get the timezone from the ip of the user
This is another method of getting the timezone of the logged in user. You can use ip of the user to track his geolocation and from that you can determine his timezone. Though, personally i don't think this is a good approach to go for. In my personal opinion it looks like an over kill unless you are already tracking and showing data on the basis of user's geolocation.

3. Get the timezone of the user through javascript.
I personally like this method because of its simplicity. You can easily get the timezone of the logged in user through following steps.
  • Get the timezone of the logged in user on your main page. For this i saw such complex approaches that i was amazed. Certainly some people love to bring gun to a knife fight :s .You can achieve this through one line of the following code
    • - (new Date.getTimezoneOffset()/60)
      • This method basically returns the difference between GMT and local time in minutes. So, if you are at GMT+2 then Date.getTimezoneOffset() will return -120 and if you are at GMT-2 then it will return 120. Therefore, simply put a - before it and divide the result by 60 and you'll get the time difference in hours. 
  • Make an ajax call and send this time difference on some server page and store it in session or cookie. A simple snippet of the code is
                         <?php 
             session_start();
             $_SESSION['userTimeZone'] = $_GET['utz'];
           ?>
Now, you have user's timezone saved in session. Whenever, you want to display a time, simply convert the stored time into user's timezone. Now, again there are multiple ways to achieve this. One very simple way is to save user's timezone in terms of seconds and simply add/subtract from the stored timestamp (if you used GMT as your default timezone). Or, you can use the following code to achieve this as well. (in order to use the following code you need to save user's timezone in proper format i.e. GMT+2)

public function convertTimeZone($date, $from_tz, $to_tz, $dateFormat) {
    $dateObj = new DateTime($date, new DateTimeZone($from_tz));
    $dateObj->setTimezone(new DateTimeZone($to_tz));
   
    return $dateObj->format($dateFormat);
}


And there you go, it's as simple as that.

Saturday, January 21, 2012

A simple jquery based ticker

This is a fairly simple and straight forward tutorial. The focus of this post is to share a simple method to create a ticker. There are many real life scenerios where you need tickers, e.g. on a news website to show the latest news. Now, keep in mind that this tutorial is not about real time event handling (though i am planning to write a tutorial on that as well). In this post, i'll simply cover a piece of code through which you can get all the data from server and then show it in the form of a automatic carousel (ticker). So, let's start with the code.

var initTicker = function(wrapperDiv, dataDiv, elementHeight, elementsToBeShown){
    $.get('getFeeds.php', function(data) {
        $('#'+dataDiv).html(data);
        var outerDiv = $('<div id="outerDiv"></div>').hide();       
        outerDiv.css({
                      'width': $("#activites").width(), 
                      'height': elementHeight*elementsToBeShown + 10,
                      'overflow': 'hidden'
                    });
        outerDiv.append($("#"+dataDiv));
        outerDiv.show();
        $("#"+wrapperDiv).append(outerDiv);         
        setInterval( function () { moveItUp(dataDiv, elementHeight) }, 7000); 
  });
}

In the above code, the data is basically provided from getFeeds.php.
wrapperDiv contains the id of the div which contains the div in which data is to be displayed. (A better approach is to create this wrapper div at run time and place the div in which data is to be shown inside the wrapper... for the sake of simplicity i am not including that code here)
dataDiv contains the id of the div in which data is to be shown
elementHeight height of one news element (again this can be determined through a simple javascript)
elementsToBeShown number of elements that you want to show on screen at a time.
 
a simple HTML snippet is


<div id="wrapper">
      <div id="activities">
      </div>
</div>

Now, before i move forward, let me just quickly explain what the above js code is doing. For explanation, i am gonna be using the divs shown in shown html snippet. A step by step explanation is:

1. Get the data from server and place the data in "activities"
2. Create a div at run time and set its width equal to the width of "activities" since we want the ticker to move vertically. Set the height of the div equal to the elementHeight*elementsToBeShown. Add some padding on the height side to get a smooth effect.. Set the overflow to hidden. Now, i'll get back to this point that why do we need the "overflow" property to be hidden.
3. Place the "activities" div inside "outerDiv"
4. Initiate the function which is supposed to apply the carousel/ticker style. Set the timer value according to your need. In the above code, the ticker will move the feeds every 7 seconds.

Now, let's quickly see what is moveItUp() is doing.

var moveItUp = function(dataDiv, elementHeight){
     var reduceMargin = '-='+elementHeight;
     $("#"+dataDiv).animate({'margin-top': reduceMargin}, 2000); 
}


Ok, this is only a two liner function. The above function is simply decreasing the "margin-top" property of "activities" div (which contains the data) by elementHeight. Now, why elementHeight and what purpose is served by decreasing "margin-top"?? The above function will successfully, move the data one element at a time. If you want to move 2 elements up, simply set reduceMargin to "-="+(2*elementHeight). Now, coming to the second part of question. If you remember, in the initialize code of "outerDiv", i mentioned to set "overflow" property to hidden. If you set "overflow" property to "hidden" of a div (or any element) then you'll see only the data which can fit in the mentioned height and width. So, for example, if a div's height and width are set to 500px, and you place data which requires 800px of height, then you'll only see 500px worth of data and rest will be clipped, there won't be any scrollbar as well. (for those who don't remember overflow property, a quick link http://www.w3schools.com/cssref/pr_pos_overflow.asp). So, in the initialization code, we set the height of the outerDiv to elementHeight*numberOfElements, and "activities" (dataDiv) contains all the data, but since "activities" is inside outerDiv, therefore, only that number of elements are shown on the screen that we passed to initTicker function, rest are clipped (not shown). Now, when we decrease the "margin-top" of "activities" div, it basically moves the data inside div vertically, but since, outerDiv can only contain "numberOfElements" elements on the screen at a time, the next elements in the "activities" div show up and the elements at top disappear. Using the jquery function "animate" we give the margin-top reduction step some nice animation and hence you get an effect of ticker/carousel.