<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.2.3" -->
<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/"
	>

<channel>
	<title>PHP-Scripts Blog &#187; Date and Time</title>
	<link>http://www.php-scripts.com</link>
	<description>Writing about PHP scripting since 12/99. Learn something new every day.</description>
	<pubDate>Thu, 09 Oct 2008 14:17:07 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.3</generator>
	<language>en</language>
			<item>
		<title>How to build a how many days in the future script with timezone offset</title>
		<link>http://www.php-scripts.com/20061221/98/</link>
		<comments>http://www.php-scripts.com/20061221/98/#comments</comments>
		<pubDate>Thu, 21 Dec 2006 18:31:36 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Date and Time]]></category>

		<category><![CDATA[How To]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/20061221/98/</guid>
		<description><![CDATA[This morning I was wondering what the date would be an arbitrary number of days in the future, like if somebody says: get back to me in a few months. I decided to search Google to see if there was a simple, web-based form that I could enter in the number of days in the [...]]]></description>
			<content:encoded><![CDATA[<p>This morning I was wondering what the date would be an arbitrary number of days in the future, like if somebody says: get back to me in a few months. I decided to search Google to see if there was a simple, web-based form that I could enter in the number of days in the future and my timezone and get returned the date/time in the future.</p>
<p>A couple variations of queries didn&#8217;t turn up anything that fit what I was looking for, so I decided to build my own and share the process here. For this project we&#8217;ll be using the following built-in PHP functions: </p>
<p>count - get the size of the timezonelist array<br />
time - get timestamp<br />
strtotime - add form submitted amount of time<br />
date - format the date<br />
putenv - change timezone</p>
<p><b>Data needed</b><br />
List of timezones inserted into a sorted array. I&#8217;ve done the work here and provide in an array called $timezonelist. This will be used in the dropdown input form so the person using can set to their timezone.</p>
<p><b>Security concerns</b><br />
To prevent users from entering in bogus timezones we&#8217;ll tie to a valid array index and check before using putenv() to change.</p>
<p>STEP 1. Add a comment header to explain the code and licensing terms:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="coMULTI">/* Created: 12/21/2006<br />
daysinfuture2.php -&gt; original code by TDavid @ tdscripts.com <br />
License: Creative Commons 2.5 Attribution<br />
Desscription: Enter in days in the future and find out the date<br />
*/</span></div>
</div>
<p>STEP 2. Add the sorted time zone list array and determine the array size using the count function. Due to the size of the array, I&#8217;m including only a couple entries in the code below. You&#8217;ll be able to view the entire array at the end of this post.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$timezonelist</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;Africa/Abidjan&#8217;</span>,<span class="st0">&#8216;Africa/Accra&#8217;</span><span class="br0">&#41;</span>; <span class="co1">// incomplete timezone list, example only</span><br />
<span class="re0">$sizetz</span> = <a href="http://www.php.net/count"><span class="kw3">count</span></a><span class="br0">&#40;</span><span class="re0">$timezonelist</span><span class="br0">&#41;</span>;</div>
</div>
<p>STEP 3. Handle the input from the form submit, if any and validate the input as integers. The maximum number of days allowed into the future is 10,000. </p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$fdays</span> = <span class="br0">&#40;</span>int<span class="br0">&#41;</span><span class="re0">$_POST</span><span class="br0">&#91;</span><span class="st0">&#8216;d&#8217;</span><span class="br0">&#93;</span>; <span class="co1">// number of days entered from form</span><br />
<span class="re0">$ftz</span> = <span class="br0">&#40;</span>int<span class="br0">&#41;</span><span class="re0">$_POST</span><span class="br0">&#91;</span><span class="st0">&#8216;tz&#8217;</span><span class="br0">&#93;</span>; <span class="co1">// timezone chosen from</span></p>
<p><span class="co1">// 10,000 days max</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$fdays</span> &amp;lt;<span class="nu0">0</span> || <span class="re0">$fdays</span> &gt;<span class="nu0">10000</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="re0">$fdays</span> = <span class="nu0">7</span>; <span class="co1">// week from now is default</span><br />
<span class="br0">&#125;</span></p>
<p><span class="co1">// default timezone is Los Angeles / PST</span><br />
<span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$ftz</span> &gt; <span class="re0">$sizetz</span> || <span class="re0">$ftz</span> &lt; <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="re0">$ftz</span> = <span class="nu0">107</span>; <span class="co1">// default timezone PST</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>STEP 4. Set the timezone using the index in the timezonelist array chosen by the person submitting the form or the default if an invalid or no selection was made.</p>
<div class="codesnip-container" >
<div class="codesnip"><a href="http://www.php.net/putenv"><span class="kw3">putenv</span></a><span class="br0">&#40;</span><span class="st0">&#8220;TZ=$timezonelist[$ftz]&#8221;</span><span class="br0">&#41;</span>;</div>
</div>
<p>STEP 5. Get the current time and calculate the number of days into the future.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="re0">$today</span> = <a href="http://www.php.net/time"><span class="kw3">time</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="re0">$future_tense</span> = <a href="http://www.php.net/strtotime"><span class="kw3">strtotime</span></a><span class="br0">&#40;</span><span class="st0">&#8220;+$fdays days&#8221;</span>, <span class="re0">$today</span><span class="br0">&#41;</span>;</div>
</div>
<p>STEP 6. Output to the browser the current formatted date/time and future date/time. Then close the PHP script.</p>
<div class="codesnip-container" >
<div class="codesnip"><a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;Now: &#8220;</span> . <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#8220;l M d, Y H:i:s&#8221;</span>, <span class="re0">$today</span><span class="br0">&#41;</span> . <span class="st0">&#8221; &lt;i&gt;$timezonelist[$ftz]&lt;br /&gt;&lt;font color=<span class="es0">\&#8221;</span>green<span class="es0">\&#8221;</span>&gt;&lt;b&gt;+ $fdays days =&lt;/b&gt;&lt;/font&gt; &lt;b&gt;&#8221;</span> . <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#8220;l M d, Y H:i:s&#8221;</span>, <span class="re0">$future_tense</span><span class="br0">&#41;</span> . <span class="st0">&#8216;&lt;/b&gt;&#8217;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p>STEP 7.  Create the first part of the form for the person to submit to the script the days into  the future and timezone. At the top of the list select the USA Los Angeles time (PST) as default. The values for the options are the index position in the timezone array.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="sc2"><a href="http://december.com/html/4/element/form.html"><span class="kw2">&lt;form</span></a> <span class="kw3">method</span>=<span class="st0">&#8220;POST&#8221;</span> <span class="kw3">action</span>=<span class="st0">&#8220;daysinfuture2.php&#8221;</span><span class="kw2">&gt;</span></a></span><br />
Number of days in the future? <span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> <span class="kw3">type</span>=<span class="st0">&#8220;text&#8221;</span> <span class="kw3">size</span>=<span class="st0">&#8220;3&#8243;</span> <span class="kw3">name</span>=<span class="st0">&#8220;d&#8221;</span> <span class="kw3">value</span>=<span class="st0">&#8220;&lt;?php echo($fdays);?/&gt;</span>&quot;&gt; Timezone? (pending) <br />
<span class="sc2">&lt;select name=&#8221;</span>tz<span class="st0">&#8220;&gt;</span><br />
<span class="sc2">&lt;option SELECTED value=&#8221;</span>107<span class="st0">&#8220;&gt;</span>USA - Pacific (GMT -8)<span class="sc2">&lt;/option&gt;</span><br />
<span class="sc2">&lt;option value=&#8221;</span>70<span class="st0">&#8220;&gt;</span>USA - Midwest (GMT -6)<span class="sc2">&lt;/option&gt;</span><br />
<span class="sc2">&lt;option value=&#8221;</span>122<span class="st0">&#8220;&gt;</span>USA - Eastern (GMT -5)<span class="sc2">&lt;/option&gt;</span> </span></div>
</div>
<p>STEP 8. Dynamically generate all sorted timezones from the array.</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="kw1">for</span><span class="br0">&#40;</span><span class="re0">$y</span>=<span class="nu0">0</span>;<span class="re0">$y</span>&lt;<span class="re0">$sizetz</span>;<span class="re0">$y</span>++<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a><span class="br0">&#40;</span><span class="st0">&#8220;&lt;option value=<span class="es0">\&#8221;</span>$y<span class="es0">\&#8221;</span>&gt;$timezonelist[$y]<span class="es0">\n</span>&#8220;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p>STEP 9. Now close the HTML form code and we&#8217;re done.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="sc2"><span class="kw2">&lt;/select&gt;</span></span> <span class="sc2"><a href="http://december.com/html/4/element/input.html"><span class="kw2">&lt;input</span></a> <span class="kw3">type</span>=<span class="st0">&#8220;submit&#8221;</span> <span class="kw3">value</span>=<span class="st0">&#8220;See future date&#8221;</span>/<span class="kw2">&gt;</span></a></span><br />
<span class="sc2"><span class="kw2">&lt;/form&gt;</span></span></div>
</div>
<p>Test execution of <a href="http://www.php-scripts.com/examples/daysinfuture2.php">daysinfuture2.php</a><br />
Complete <a href="http://www.php-scripts.com/examples/daysinfuture2.phps">source code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20061221/98/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to change background color automatically on holidays</title>
		<link>http://www.php-scripts.com/20051018/45/</link>
		<comments>http://www.php-scripts.com/20051018/45/#comments</comments>
		<pubDate>Tue, 18 Oct 2005 14:06:20 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Date and Time]]></category>

		<category><![CDATA[How To]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/?p=45</guid>
		<description><![CDATA[Ever notice how Google changes its logo on different holidays throughout the year? I decided this morning I wanted to change the background color and some other themes on one of our websites.
With Halloween, Thanksgiving and Christmas coming I added some code this morning to one of our sites for implementing different background/theme switching so [...]]]></description>
			<content:encoded><![CDATA[<p>Ever notice how <a href="http://www.google.com/intl/en/holidaylogos.html ">Google changes its logo</a> on different holidays throughout the year? I decided this morning I wanted to change the background color and some other themes on one of our websites.</p>
<p>With Halloween, Thanksgiving and Christmas coming I added some code this morning to one of our sites for implementing different background/theme switching so that it would automatically change the color scheme across the website during the holiday.</p>
<p>I did something similar to change the background color every day of the week in my <a href="http://www.php-scripts.com/php_diary/121999.html">PHP Diary: December 19, 1999</a> (and then the follow-up on <a href="http://www.php-scripts.com/php_diary/122099.php3">December 20, 1999</a> a better version using an array instead of if/else statements). That background changing script has been running pretty much unmodified every day for nearly six years now.</p>
<p><b>How to change the background color of an HTML page only on Halloween and Christmas day</b></p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="re0">$background_color</span> = <span class="st0">&#8216;#FFF&#8217;</span>; <span class="co1">// default WHITE background</span><br />
<span class="kw1">switch</span><span class="br0">&#40;</span><a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#8220;md&#8221;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw1">case</span> <span class="nu0">1031</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// automatic halloween background color (light orange) on 10/31</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">$background_color</span> = <span class="st0">&#8216;#FFE6CC&#8217;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">break</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">case</span> <span class="nu0">1225</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// xmas day background color (light green) on 12/25</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="re0">$background_color</span> = <span class="st0">&#8216;#D5FFD5&#8242;</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="kw1">break</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">?&gt;</span></p>
<p>&lt;body bgcolor=<span class="st0">&#8220;&lt;?php echo($background_color);?&gt;&#8221;</span>&gt;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p>To modify the script for other colors and holidays just add additional case blocks like say we wanted the background to be red for 4th of the July we&#8217;d add the following case block:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw1">case</span> <span class="nu0">0704</span>:<br />
<span class="re0">$background_color</span> = <span class="st0">&#8216;red&#8217;</span>;<br />
<span class="kw1">break</span>;</div>
</div>
<p>And so on.</body></p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20051018/45/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

