Update @ 2012-09-25: This approach could not handle the DST zones. Thanks for the comment made by Lucas. =)
You can make use the TimeHelper to offset the time by user specified time zone. What u need is adding a new time_zone column in the users table just like the following example.
--MySQL CREATE TABLE IF NOT EXISTS users ( user_id INT NOT NULL auto_increment, name VARCHAR(45) NOT NULL, password VARCHAR(45) NOT NULL, time_zone DECIMAL(5,1) NOT NULL, created DATETIME DEFAULT NULL, PRIMARY KEY (user_id) ) ENGINE = InnoDB;
So now you can set the time zone for each user. As i am living in Hong Kong, my time zone should be GMT+8:00. so my time_zone column will be 8.0. If you are in GMT-2:30, the time_zone column will be -2.5.
It is now ready to apply the time zone in the view. First, you have to add the TimeHelper in the controller.
var $helpers = array('Time');
In the view, say, i want to offset the created field by my time zone. Add the following line.
<?php echo $time->format('Y-m-d H:i:s', $user['User']['created'], null, $user['User']['TimeZone']); ?>
Done =)
Reference: Planet CakePHP – User Timezones in CakePHP
Use `timestamp` column, and set your users timezone upon login is a better approach IMO.
LikeLike
Thanks for your suggestion, Rachman.
I will take a look on the timestamp approach later. =)
LikeLike
Any time you’re using fixed offsets for a timezone, you may run into trouble. How does this cope with DST?
LikeLike
I never thought about that, maybe run a sql update on the time_zone column for those DST zones.
Thanks for your comment anyway, i have added an reminder on this post. =)
LikeLike
See: http://infiniteundo.com/post/25509354022/more-falsehoods-programmers-believe-about-time-wisdom
LikeLike
Thanks again for the link. It is a good read which could clarify the concepts about time. =D
LikeLike