<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Trent Richardson &#187; CakePHP</title> <atom:link href="http://trentrichardson.com/category/cakephp/feed/" rel="self" type="application/rss+xml" /><link>http://trentrichardson.com</link> <description>practical web design &#38; development</description> <lastBuildDate>Wed, 25 Jan 2012 11:43:16 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Handling TimeZones in CakePHP</title><link>http://trentrichardson.com/2010/03/05/handling-timezones-in-cakephp/</link> <comments>http://trentrichardson.com/2010/03/05/handling-timezones-in-cakephp/#comments</comments> <pubDate>Fri, 05 Mar 2010 13:15:02 +0000</pubDate> <dc:creator>trent</dc:creator> <category><![CDATA[CakePHP]]></category> <category><![CDATA[Database]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[convert]]></category> <category><![CDATA[date]]></category> <category><![CDATA[datetime]]></category> <category><![CDATA[time]]></category> <category><![CDATA[timezone]]></category> <category><![CDATA[user]]></category><guid
isPermaLink="false">http://trentrichardson.com/?p=526</guid> <description><![CDATA[The web is a global resource for anything, anywhere, any timezone. But often times we are careless in consideration for people in other areas of the world. The different timezones offset people&#8217;s schedules from one area to the next. When handling user profiles (or anything for that matter) where date and time is involved we [...]
Related posts:<ol><li><a
href='http://trentrichardson.com/2009/03/31/handling-php-form-arrays-with-jquery/' rel='bookmark' title='Handling PHP Form Arrays with jQuery'>Handling PHP Form Arrays with jQuery</a></li><li><a
href='http://trentrichardson.com/2011/12/02/discussion-how-asynchronous-should-we-get/' rel='bookmark' title='Discussion &#8211; How Asynchronous Should We Get'>Discussion &#8211; How Asynchronous Should We Get</a></li><li><a
href='http://trentrichardson.com/2010/01/31/cakephp-postgresql-and-regex/' rel='bookmark' title='CakePHP, PostgreSQL, and Regex'>CakePHP, PostgreSQL, and Regex</a></li></ol>]]></description> <content:encoded><![CDATA[<p><img
src="http://trentrichardson.com/wp-content/uploads/2010/03/timezone.png" alt="Timezones with CakePHP" title="timezone" width="128" height="128" class="alignright size-full wp-image-530" /></p><p>The web is a global resource for anything, anywhere, any timezone.  But often times we are careless in consideration for people in other areas of the world.  The different timezones offset people&#8217;s schedules from one area to the next.  When handling user profiles (or anything for that matter) where date and time is involved we can do a little magic with our CakePHP app to make things a little more convenient for our users.  How do we handle this you ask?</p><p>First lets say when a user logs in to their profile they can click to edit the profile, selects his/her timezone, and clicks save. Relative to GMT we can calculate offsets of each timezone, where GMT would = 0.  So for instance Eastern Timezone for New York, US would be -5 since its 5 ours BEHIND GMT.  So lets start by generating a dropdown list with cake with all the different timezones, then save this list to the users profile.  If you are using Auth with CakePHP, you will likely have a users table, and we&#8217;ll add a decimal field called &#8220;timezone&#8221; to that table.  Fortunately there is a helper to assist us with this dropdown in the bakery, <a
href="http://bakery.cakephp.org/articles/view/updated-timezone-helper">Timezone Helper</a>.  By taking a look at the source you&#8217;ll notice all the labels associated with an offset &#8220;-5.0&#8243; or &#8220;+3.0&#8243;.  Also look a little closer and you might notice a few have actual decimal (Kabul is +4.5).  So first things first, go ahead and install that Helper as those instructions say.  Add it to your Edit view for users to edit their profile. Once included you just add the drop down to your form with:</p><pre><code class="php">
echo $timezone->select('timezone');
</code></pre><p>Now each user should have a timezone attached to their profile.  Now the trick to timezones is that no matter what the timezone of our server is, we just want to convert it to display to the user in that user&#8217;s preferred timezone.  On the reverse side, if a user enters a datetime, we convert it back to the timezone of the server.</p><p>Lets start our with the user viewing a date from the server, in our view we want to convert it to the user&#8217;s preferred timezone set to their profile (which should now be stored in their session, I&#8217;ll demonstrate with Auth)</p><pre><code class="php">
$time->format('F j, Y g:i:s a', $mydate, null, $session->read('Auth.User.timezone'));
</code></pre><p>Using the Time helper in CakePHP, the format function accepts a fourth parameter which indicates the timezone, which is extremely helpful.  So this is all you need to do to display a time from the server to a user.</p><p>Next lets say the user is about to edit a field from the server in a form.  First we need to select the existing data from the database, we&#8217;ll convert it to the user&#8217;s timezone, and place it in an input field to be edited.  Then once the user saves the data we&#8217;ll simply convert it back to the server&#8217;s timezone.  (There a bunch of &#8220;one liners&#8221; in doing all of these tasks).  So if you have your Time helper in your controller when you get the data from the database, go ahead and convert it to the new timezone just like we did before:</p><pre><code class="php">
$data['Model']['mydate'] = $time->format('Y-n-d H:i:sP', $data['Model']['mydate'], null, $this->Auth->user('timezone'));
</code></pre><p>Now in your view for the form there are a few different scenarios.  1) You use Cake&#8217;s default inputs for editing date times, with the 6 dropdowns, or 2) You use a plain input and use a date/time picker of some sort.  Lets first handle the first scenario, Cakes default 6 input datetime.  The data has been submitted, validated and we&#8217;re about to place it back in the database, we need to convert it back to server timezone.  But wait! the form field is broken down into an array of six elements: year, month, day, hour, minute, meridian.  Well we can create a nice little function that will convert this array into a datetime, and pass it our timezone:</p><pre><code class="php">
function dateTimeArrayToServerTZString($dt, $tz=0){
	$tz = (floatval($tz) >= 0)? '+'. number_format(floatval($tz),2,':','') : number_format(floatval($tz),2,':','');
	$dt['min'] = str_pad($dt['min'], 2, "0", STR_PAD_LEFT);

	$loc = $dt['year'] .'-'. $dt['month'] .'-'. $dt['day'] .' '. $dt['hour'] .':'. $dt['min'] .' '. $dt['meridian'] .' '. $tz;

	$datetime = new DateTime($loc);
	$datetime->setTimezone(new DateTimeZone(date('e', time())));

	return $datetime->format('Y-n-d H:i:sP');
}
</code></pre><p>Now right before I insert my data into the database I just need to call this function on that datetime array:</p><pre><code class="php">
$this->data['Model']['mydate'] = dateTimeArrayToServerTZString($this->data['Model']['mydate'], $this->Auth->user('timezone'));
</code></pre><p>Now $this->data['Model']['mydate'] will be a string, and cake still knows how to handle that just fine.  Now for the second scenerio, a user submitting a string.  This isn&#8217;t too hard at all, we just use our time helper and convert that puppy back to the server time.</p><pre><code class="php">
$this->data['Model']['mydate'] = $time->format('Y-n-d H:i:sP', $this->data['Model']['mydate'], null, -5);
</code></pre><p>Ok, so I&#8217;ve hardcoded in the timezone to -5 (Eastern Timezone), what if I want this to be dynamic to where ever my server is? Well the Time helper has a function which isn&#8217;t mentioned in the book called serverOffset(), which will return a number of the offset.  If you&#8217;re not using the helper and just want to get the timezone of the server its a simple one-liner:</p><pre><code class="php">
date('Z', time())
</code></pre><p>Thats all there is to handling timezones in your cakePHP site, or any site for that matter.  Just keep in mind as you go to convert any times for the user&#8217;s pleasure.  You have any tips or alternatives?</p><p>Related posts:<ol><li><a
href='http://trentrichardson.com/2009/03/31/handling-php-form-arrays-with-jquery/' rel='bookmark' title='Handling PHP Form Arrays with jQuery'>Handling PHP Form Arrays with jQuery</a></li><li><a
href='http://trentrichardson.com/2011/12/02/discussion-how-asynchronous-should-we-get/' rel='bookmark' title='Discussion &#8211; How Asynchronous Should We Get'>Discussion &#8211; How Asynchronous Should We Get</a></li><li><a
href='http://trentrichardson.com/2010/01/31/cakephp-postgresql-and-regex/' rel='bookmark' title='CakePHP, PostgreSQL, and Regex'>CakePHP, PostgreSQL, and Regex</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://trentrichardson.com/2010/03/05/handling-timezones-in-cakephp/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Using Cakephp Components in Shells and Tasks</title><link>http://trentrichardson.com/2010/02/15/using-cakephp-components-in-shells-and-tasks/</link> <comments>http://trentrichardson.com/2010/02/15/using-cakephp-components-in-shells-and-tasks/#comments</comments> <pubDate>Mon, 15 Feb 2010 13:04:00 +0000</pubDate> <dc:creator>trent</dc:creator> <category><![CDATA[CakePHP]]></category><guid
isPermaLink="false">http://trentrichardson.com/?p=485</guid> <description><![CDATA[Not sure if cake has a prettier, more efficient way of grabbing components inside of Shells and Tasks with cakephp&#8217;s cli, but here is a way which worked for me. Inside your Task (or Shell) just use the following in a similar manner for your component: class FooTask extends Shell { App::import('Component', 'Bar'); $this->Bar =&#038; [...]
Related posts:<ol><li><a
href='http://trentrichardson.com/2010/02/03/adding-postgresql-regex-support-in-cakephp/' rel='bookmark' title='Adding PostgreSQL Regex Support in CakePHP'>Adding PostgreSQL Regex Support in CakePHP</a></li><li><a
href='http://trentrichardson.com/2010/01/31/cakephp-postgresql-and-regex/' rel='bookmark' title='CakePHP, PostgreSQL, and Regex'>CakePHP, PostgreSQL, and Regex</a></li><li><a
href='http://trentrichardson.com/2010/03/05/handling-timezones-in-cakephp/' rel='bookmark' title='Handling TimeZones in CakePHP'>Handling TimeZones in CakePHP</a></li></ol>]]></description> <content:encoded><![CDATA[<p>Not sure if cake has a prettier, more efficient way of grabbing components inside of Shells and Tasks with cakephp&#8217;s cli, but here is a way which worked for me.  Inside your Task (or Shell) just use the following in a similar manner for your component:</p><pre><code class="php">
class FooTask extends Shell {
	App::import('Component', 'Bar');

	$this->Bar =&#038; new BarComponent();

	$resp = $this->Bar->crazy_things();
	$this->out($resp);
}
</code></pre><p>Of course Foo and Bar are just for example.  This works for me.  Anyone know of another way?</p><p>Related posts:<ol><li><a
href='http://trentrichardson.com/2010/02/03/adding-postgresql-regex-support-in-cakephp/' rel='bookmark' title='Adding PostgreSQL Regex Support in CakePHP'>Adding PostgreSQL Regex Support in CakePHP</a></li><li><a
href='http://trentrichardson.com/2010/01/31/cakephp-postgresql-and-regex/' rel='bookmark' title='CakePHP, PostgreSQL, and Regex'>CakePHP, PostgreSQL, and Regex</a></li><li><a
href='http://trentrichardson.com/2010/03/05/handling-timezones-in-cakephp/' rel='bookmark' title='Handling TimeZones in CakePHP'>Handling TimeZones in CakePHP</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://trentrichardson.com/2010/02/15/using-cakephp-components-in-shells-and-tasks/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 2/31 queries in 0.010 seconds using disk: basic
Object Caching 723/753 objects using disk: basic

Served from: trentrichardson.com @ 2012-02-03 22:25:30 -->
