CakePHP – Apply Time Zone for Users

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

Advertisements

6 thoughts on “CakePHP – Apply Time Zone for Users”

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

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s