<?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; one liners</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>Date and time page last updated</title>
		<link>http://www.php-scripts.com/20070329/104/</link>
		<comments>http://www.php-scripts.com/20070329/104/#comments</comments>
		<pubDate>Thu, 29 Mar 2007 16:22:05 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[one liners]]></category>

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

		<guid isPermaLink="false">http://www.php-scripts.com/20070329/104/</guid>
		<description><![CDATA[A PHP one-liner to keep the date/time the page was last updated:

page last updated &#60;?php echo date&#40;&#8220;F d Y H:i:s&#8221;, getlastmod&#40;&#41; &#41;; ?&#62;

Note: uses the server timezone by default. getlastmod() returns a timestamp and if you are on a shared server (virtual hosting) and cannot change the timezone, let&#8217;s say the timezone is off by [...]]]></description>
			<content:encoded><![CDATA[<p>A PHP one-liner to keep the date/time the page was last updated:</p>
<div class="codesnip-container" >
<div class="codesnip">page last updated &lt;?php <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#8220;F d Y H:i:s&#8221;</span>, <a href="http://www.php.net/getlastmod"><span class="kw3">getlastmod</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>; <span class="kw2">?&gt;</span></div>
</div>
<p>Note: uses the server timezone by default. getlastmod() returns a timestamp and if you are on a shared server (virtual hosting) and cannot change the timezone, let&#8217;s say the timezone is off by two hours just use the following:</p>
<div class="codesnip-container" >
<div class="codesnip">page last updated &lt;?php <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#8220;F d Y H:i:s&#8221;</span>, <span class="br0">&#40;</span><a href="http://www.php.net/getlastmod"><span class="kw3">getlastmod</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span> - <span class="nu0">7200</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>; <span class="kw2">?&gt;</span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20070329/104/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Random web page background colors</title>
		<link>http://www.php-scripts.com/20060714/89/</link>
		<comments>http://www.php-scripts.com/20060714/89/#comments</comments>
		<pubDate>Fri, 14 Jul 2006 13:51:01 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Random]]></category>

		<category><![CDATA[one liners]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/20060714/89/</guid>
		<description><![CDATA[If you want to create a random background color

&#60;?php $hex = dechex&#40;rand&#40;0,255&#41;&#41; . dechex&#40;rand&#40;0,255&#41;&#41; . dechex&#40;rand&#40;0,255&#41;&#41;; ?&#62;

and then you&#8217;d use the following in your body tag:

&#60;body BGCOLOR=&#8220;#&#60;?php echo($hex);?&#62;&#8221;&#62;

Related
How to change background color only on holidays
]]></description>
			<content:encoded><![CDATA[<p>If you want to create a random background color</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php <span class="re0">$hex</span> = <a href="http://www.php.net/dechex"><span class="kw3">dechex</span></a><span class="br0">&#40;</span><a href="http://www.php.net/rand"><span class="kw3">rand</span></a><span class="br0">&#40;</span><span class="nu0">0</span>,<span class="nu0">255</span><span class="br0">&#41;</span><span class="br0">&#41;</span> . <a href="http://www.php.net/dechex"><span class="kw3">dechex</span></a><span class="br0">&#40;</span><a href="http://www.php.net/rand"><span class="kw3">rand</span></a><span class="br0">&#40;</span><span class="nu0">0</span>,<span class="nu0">255</span><span class="br0">&#41;</span><span class="br0">&#41;</span> . <a href="http://www.php.net/dechex"><span class="kw3">dechex</span></a><span class="br0">&#40;</span><a href="http://www.php.net/rand"><span class="kw3">rand</span></a><span class="br0">&#40;</span><span class="nu0">0</span>,<span class="nu0">255</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="kw2">?&gt;</span></div>
</div>
<p>and then you&#8217;d use the following in your body tag:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;body BGCOLOR=<span class="st0">&#8220;#&lt;?php echo($hex);?&gt;&#8221;</span>&gt;</div>
</div>
<p><b>Related</b><br />
<a href="http://www.php-scripts.com/20051018/45/">How to change background color only on holidays</a></body></p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20060714/89/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Is number odd or even?</title>
		<link>http://www.php-scripts.com/20051104/57/</link>
		<comments>http://www.php-scripts.com/20051104/57/#comments</comments>
		<pubDate>Fri, 04 Nov 2005 18:13:20 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[one liners]]></category>

		<category><![CDATA[Functions]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/?p=57</guid>
		<description><![CDATA[Testing whether a number is odd or even in PHP. The custom function is_odd() below is inspired by an algorithms thread at Devshed. The function isn&#8217;t even necessary and could be expressed as a one-liner like this:

echo&#40;$number &#38; 1&#41;; // $number = any integer, 0 = even, 1 = odd 
&#160;

Here&#8217;s the code to check [...]]]></description>
			<content:encoded><![CDATA[<p>Testing whether a number is odd or even in PHP. The custom function is_odd() below is inspired by an <a href="http://forums.devshed.com/archive/t-29843/Quick-algorithm-to-determine-oddeven-numbers*/">algorithms thread at Devshed</a>. The function isn&#8217;t even necessary and could be expressed as a one-liner like this:</p>
<div class="codesnip-container" >
<div class="codesnip"><a href="http://www.php.net/echo"><span class="kw3">echo</span></a><span class="br0">&#40;</span><span class="re0">$number</span> &amp; <span class="nu0">1</span><span class="br0">&#41;</span>; <span class="co1">// $number = any integer, 0 = even, 1 = odd </span><br />
&nbsp;</div>
</div>
<p>Here&#8217;s the code to check the hours in the day starting with 0 (midnight) and ending at midnight (23):</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">function</span> is_odd<span class="br0">&#40;</span><span class="re0">$number</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp;<span class="kw1">return</span> <span class="re0">$number</span> &amp; <span class="nu0">1</span>; <span class="co1">// 0 = even, 1 = odd</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw1">foreach</span><span class="br0">&#40;</span><a href="http://www.php.net/range"><span class="kw3">range</span></a><span class="br0">&#40;</span><span class="nu0">0</span>,<span class="nu0">23</span><span class="br0">&#41;</span> <span class="kw1">as</span> <span class="re0">$number</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="st0">&#8220;$number &#8220;</span> . is_odd<span class="br0">&#40;</span><span class="re0">$number</span><span class="br0">&#41;</span> . <span class="st0">&#8216;&lt;br /&gt;&#8217;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Notes: The range function is handy for prefilling with a string or array with numbers. Now what if you want to show alternating messages, one on the even hours and one on odd hours? The following code will do just that if you change the print statement to be whatever you want to alternate:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="kw2">function</span> is_odd<span class="br0">&#40;</span><span class="re0">$number</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <span class="kw1">return</span> <span class="re0">$number</span> &amp; <span class="nu0">1</span>; <span class="co1">// 0 = even, 1 = odd</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw1">if</span><span class="br0">&#40;</span>is_odd<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;H&#8221;</span><span class="br0">&#41;</span><span class="br0">&#41;</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="st0">&#8220;Since it&#8217;s an odd hour, I&#8217;ve got an odd message for you: read and be happy&#8221;</span>;<br />
<span class="br0">&#125;</span> <span class="kw1">else</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="st0">&#8220;It&#8217;s an even hour, Steven.&#8221;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p><b>Update 10/9/08 7:16am PST</b>: Thanks to several commenters below who pointed out that there was a missing &#8216;)&#8217; &#8212; it has been added now.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20051104/57/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
