Wednesday 13 March 2013

Working with timezone and hourly time in PHP

PHP is a very useful as well as interesting language for developers. I personally love working with PHP because it is completely hassle free and gives enough flexibility with the syntax too. 
Anyways, in this post I would like to mention to all of you guys out there who are associated with web development that, my latest project was something quite close knit to time zones of the world. I was totally freaked out when I first understood the concept and had to start off. But, later on I figured out a simpler way to deal with time zones and several different hourly slots of the day.



The concept of this website is to bring together students and teachers from around the world for paid tuition online. So, I had to deal with all possible time zones of the world and bring them to a common point.


Collecting the time zones


This was one of the foremost challenges in my project. I just could not find one site that provides a perfect solution for my project. I needed a database list that would contain records of the various time zones of the world with their respective PHP time zones. Since I could not afford a paid IP tracker system, I had to go for other options. So, I collected the timezone from the user as a form input and made a function for myself. This defined function was helping me to generate the PHP timezone from the given user input. So, for example, if I was the user and I entered ("GMT +5.30 - Kolkata") in the form, then the function would automatically store it in the user's database profile as "Asia/Kolkata". The latter is the format PHP understands while setting time zones to the server. 


Identifying and handling hourly slots


As I have already mentioned earlier, the site was all about time slots. So, hourly slot divisions and identifying them was a huge challenge. Because, even if I divide the day into 24 hourly slots, the codes could go wrong at any point of time. Suppose I select a slot ( say 6:00 to 7:00 ) and convert it into a development friendly slot number ( if 12:00 is slot 1, then this would be slot number 7 ). Now, a person sitting in America would see the same slot as a different time period of the day. 
The first thing I did was to use PHP inbuilt function date_default_timezone_set(). This would set the specific time zone for every user whenever he/ she logs in. The respective timezone would be fetched from the database according to the first form input. 
Next, I stored the hourly time values in UTC. Latest versions of MySQL stores Datetime values converted to UTC, however, I did that for myself. So, whenever a teacher or student, no matter from which time zone of the world, was accessing any slot time; he or she was accessing the time in UTC.   


Displaying in user's desired timezone profile


When the database was holding the time in UTC, it became a bit easier for me to retrieve values. I created two functions. These two functions were namely for storage and retrieval of time and date from the database.   Since the timezone could vary up to 12 hours or so, another problem to be solved was the date. 

Finding dates according to the hours


As I mentioned earlier, there were two functions I defined. Both were similar in structure but did just the opposite of each other. Both were supplied with three parameters - the user's time offset, a time slot and a date (in string / timestamp). One function used this offset, the supplied slot time and date (which were in the set timezone) to convert the date and time in UTC. So, the UTC time slot changed accordingly (either hours got added, or subtracted). Once the time slot changed, the function's second job was to determine if this slot was moving below this date's first slot (if yes then the supplied date is reduced by one day) or moving above this date's last slot ( a day gets added in this case). 
The second function had the same working principle, except that it worked in the opposite mannerism. Its work was to change the slot and date from UTC to the current set timezone. 


Finally, with the help of these methodologies, I could track the exact date and time slot irrelevant of the timezone of multiple users. The last spark was to determine the day's slot as a string. This was just a small part. I created another function with an array containing slot number as "value" and the string as "key"

Hope this works out for all you guys and hope this comes into help at some point of your work. 











    

No comments:

Post a Comment

Disqus for Development Tutorials