<?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>Chuck&#039;s Musings &#187; gmdate</title>
	<atom:link href="http://blog.chuckcerrillo.com/tag/gmdate/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.chuckcerrillo.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 21 Feb 2010 18:43:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Juggling time zones</title>
		<link>http://blog.chuckcerrillo.com/2009/09/juggling-time-zones/</link>
		<comments>http://blog.chuckcerrillo.com/2009/09/juggling-time-zones/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 00:30:14 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[PHP and MySQL]]></category>
		<category><![CDATA[gmdate]]></category>
		<category><![CDATA[gmt]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[offsets]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[strtotime]]></category>
		<category><![CDATA[timestamp]]></category>
		<category><![CDATA[timezones]]></category>
		<category><![CDATA[utc]]></category>

		<guid isPermaLink="false">http://blog.chuckcerrillo.com/?p=100</guid>
		<description><![CDATA[If you're thinking of writing calendars, you better watch out for timezones.]]></description>
			<content:encoded><![CDATA[<p>Handling times for different timezones is one of the usual things I had to face with every project that has in one way or the other involves the element of time.</p>
<p>The usual projects I work on store dates into a <code class="codecolorer sql default"><span class="sql">datetime</span></code> field in the database. This means that the date must use the <code class="codecolorer text default"><span class="text">YYYY-MM-DD HH:MM:SS</span></code> format (e.g. 2009-09-03 07:45:00).</p>
<p>One of the first things you&#8217;d want to do is to let the user specify a timezone setting for that user account (assuming this is in a multi-user environment, as most web applications are). You could view all the timezones using this code:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;ul&gt;'</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span>DateTimeZone<span style="color: #339933;">::</span><span style="color: #004000;">listAbbreviations</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$timezone_abbreviation</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$timezones</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$timezones</span> <span style="color: #b1b100;">AS</span> <span style="color: #000088;">$timezone</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #990000;">isset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$timezone</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'timezone_id'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;li&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$timezone_abbreviation</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' - '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$timezone</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'timezone_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;ul&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;li&gt;Uses DST: '</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$timezone</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dst'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">===</span><span style="color: #009900; font-weight: bold;">TRUE</span>?<span style="color: #0000ff;">'Yes'</span><span style="color: #339933;">:</span><span style="color: #0000ff;">'No'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/li&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;li&gt;Offset: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$timezone</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'offset'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/li&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/ul&gt;&lt;/li&gt;'</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/ul&gt;'</span><span style="color: #339933;">;</span></div></div>
<p>You could modify the code above to create a drop-down list of the different timezones to make it easier for the user to pick a timezone (because simply writing GMT+8 or UTC+8 might be confusing for some people).</p>
<p>Next, we will have to convert all of our stored dates into a single timezone, for simplicity, I use GMT/UTC as a reference timezone (since it is easy to add timezone offsets to it). If you store your dates in a <code class="codecolorer sql default"><span class="sql">datetime</span></code> field, you can perform the following conversion after retrieving the timestamp from the database:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// let's assume $parsedtime is the entry we have in the database</span><br />
<span style="color: #666666; font-style: italic;">// and $offset is the timezone offset we got based on the user's settings</span><br />
<span style="color: #000088;">$parsedtime</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'2009-09-15 12:00:00'</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$offset</span> <span style="color: #339933;">=</span> 8<span style="color: #339933;">*</span>60<span style="color: #339933;">*</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this is GMT +8</span><br />
<span style="color: #666666; font-style: italic;">// get the GMT counterpart of the parsedtime</span><br />
<span style="color: #000088;">$unix_time</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/strtotime"><span style="color: #990000;">strtotime</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parsedtime</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' GMT'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span> <a href="http://www.php.net/gmdate"><span style="color: #990000;">gmdate</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'m d Y h:iA'</span><span style="color: #339933;">,</span><span style="color: #000088;">$unix_time</span><span style="color: #339933;">-</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// displays: 09 15 2009 04:00AM</span></div></div>
<p>So that means, 12nn of September 15, 2009 is 4am of September 15, 2009 in GMT/UTC.</p>
<p>Let&#8217;s say we had a Calendar application, and you wanted to set an alarm for 6pm on that same day. You could do the something like the this (assuming you wrote some sort of class for this thing):</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$reminder</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Reminder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'2009-09-15 18:00:00'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Dinner with the family'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$reminder</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timeLeft</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Since the current date and time was 12nn of September 15, 2009, this means that the above reminder will display &#8220;6 hours left&#8221;, depending on how the <code class="codecolorer php default"><span class="php">timeLeft<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></span></code> method is formatted.</p>
<p>If we didn&#8217;t convert the date into a standard timezone, this will force our functions to use the server&#8217;s timezone setting and that might not be what you want.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chuckcerrillo.com/2009/09/juggling-time-zones/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

