<?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; PHP 4.x</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>Open source phpMyEdit</title>
		<link>http://www.php-scripts.com/20061112/95/</link>
		<comments>http://www.php-scripts.com/20061112/95/#comments</comments>
		<pubDate>Sun, 12 Nov 2006 14:08:29 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[PHP 4.x]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/20061112/95/</guid>
		<description><![CDATA[phpMyEdit:
generates PHP code for displaying/editing MySQL tables in HTML. All you need to do is to write a simple calling program (a utility to do this is included). It includes a huge set of table manipulation functions (record addition, change, view, copy, and removal), table sorting, filtering, table lookups, and more.
Download (gz).
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.phpmyedit.org/">phpMyEdit</a>:<br />
<blockquote>generates PHP code for displaying/editing MySQL tables in HTML. All you need to do is to write a simple calling program (a utility to do this is included). It includes a huge set of table manipulation functions (record addition, change, view, copy, and removal), table sorting, filtering, table lookups, and more.</p></blockquote>
<p><a href="http://www.phpmyedit.org/article.php?download">Download</a> (gz).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20061112/95/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Apache + PHP on Mac OS X</title>
		<link>http://www.php-scripts.com/20060405/82/</link>
		<comments>http://www.php-scripts.com/20060405/82/#comments</comments>
		<pubDate>Wed, 05 Apr 2006 18:40:50 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Installation and configuration]]></category>

		<category><![CDATA[PHP 4.x]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/?p=82</guid>
		<description><![CDATA[Though the screenshots have changed the basic process is the same, Kevin Hemenway, takes readers through how to setup an Apache server on Mac OS X and then how to turn on PHP 4.
To edit the httpd.conf I needed to do the following:
1. start Terminal by going to Utilities/Terminal
2. type the following into the terminal:
sudo [...]]]></description>
			<content:encoded><![CDATA[<p>Though the screenshots have changed the basic process is the same, Kevin Hemenway, takes readers through how to setup an <a href="http://www.oreillynet.com/pub/a/mac/2001/12/07/apache.html">Apache server on Mac OS X</a> and then how to <a href="http://www.onlamp.com/pub/a/mac/2002/01/04/apache_macosx_pt3.html#id_10386">turn on PHP 4</a>.</p>
<p>To edit the httpd.conf I needed to do the following:</p>
<p>1. start Terminal by going to Utilities/Terminal<br />
2. type the following into the terminal:</p>
<p>sudo emacs /etc/http/httpd.conf</p>
<p>3. enter in your admin password<br />
4. Inside emacs, uncomment the line</p>
<p>#LoadModule php4_module</p>
<p>to read:<br />
LoadModule php4_module</p>
<p>and change:<br />
 # AddModule mod_php4.c</p>
<p>to read:<br />
AddModule mod_php4.c</p>
<p>Then add the following in the AddType section:</p>
<p>AddType application/x-httpd-php .php<br />
AddType application/x-httpd-php-source .phps</p>
<p>5. Save and exit from emacs<br />
CTRL + x, CTRL +s  = saves changes use the following keyboard combination<br />
CTRL + x, CTRL +c  = exit</p>
<p>6. I created a phpinfo() script using the <a href="http://tacosw.com/main.php">freeware Taco editor</a>.</p>
<div class="codesnip-container" >< ?php<br />
phpinfo();<br />
?></div>
<p>7. Want to <a href="http://developer.apple.com/internet/opensource/osdb.html">install MySQL on OS X</a> too? I&#8217;ll leave that one for readers (or come back and update this entry or add a new one).</p>
<p>8. I chose to download the <a href="http://dev.mysql.com/get/Downloads/MySQL-4.1/mysql-max-4.1.18-apple-darwin8.2.0-powerpc.dmg/from/pick#mirrors">MySQL 4.1 binary here</a>. Once downloaded open the package and double click the appropriate package. I chose mysql-max-4.1.18-apple-darwin8.2.0-powerpc.pkg. It will vary based on what type of Mac you have.</p>
<p>9. After install I chose to startup the database manually using the Terminal shell commands:</p>
<div class="codesnip-container" >shell> cd /usr/local/mysql<br />
     shell> sudo ./bin/mysqld_safe<br />
     (Enter your password, if necessary)<br />
     (Press Control-Z)<br />
     shell> bg<br />
     (Press Control-D or enter &#8220;exit&#8221; to exit the shell)</p>
<p>You should be able to connect to the MySQL server, for example, by<br />
running `/usr/local/mysql/bin/mysql&#8217;.</p></div>
<p>10. Next, I added an alias for MySQL using bash so I could type &#8220;mysql&#8221; to get into the databases and &#8220;mysqladmin&#8221; to use the admin without having to CD to the full path</p>
<p>alias mysql=/usr/local/mysql/bin/mysql<br />
alias mysqladmin=/usr/local/mysql/bin/mysqladmin</p>
<p>11. Voila &#8212; in! Now time to secure the MySQL database and enter in a root password</p>
<p>mysqladmin -u root password PASSWORD_HERE</p>
<p>Just change PASSWORD_HERE to the root password you want to use.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20060405/82/feed/</wfw:commentRss>
		</item>
		<item>
		<title>AJAX MyTop</title>
		<link>http://www.php-scripts.com/20060131/78/</link>
		<comments>http://www.php-scripts.com/20060131/78/#comments</comments>
		<pubDate>Tue, 31 Jan 2006 17:34:10 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[AJAX]]></category>

		<category><![CDATA[Installation and configuration]]></category>

		<category><![CDATA[PHP 5.x]]></category>

		<category><![CDATA[PHP 4.x]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/?p=78</guid>
		<description><![CDATA[Man, how many clicks does it take to get the download these days at Sourceforge? Seems like it took me a half dozen to get to the AjaxMyTop project download.

MyTop creator Jeremy Zawodny writes:
Someone has built and AJAX powered version of mytop, the little console based MySQL monitoring tool I wrote years ago. I guess [...]]]></description>
			<content:encoded><![CDATA[<p>Man, how many clicks does it take to get the download these days at Sourceforge? Seems like it took me a half dozen to get to the <a href="http://jeremy.zawodny.com/blog/archives/006199.html">AjaxMyTop project download</a>.</p>
<p><img src="http://www.php-scripts.com/images/2006/ajaxmytop.jpg" border="0"/></p>
<p>MyTop creator Jeremy Zawodny <a href="http://jeremy.zawodny.com/blog/archives/006199.html">writes</a>:<br />
<blockquote>Someone has built and AJAX powered version of mytop, the little console based MySQL monitoring tool I wrote years ago. I guess it&#8217;s now buzzword compliant.</p></blockquote>
<p><a href="http://jeremy.zawodny.com/mysql/mytop/">mytop</a> is a handy little program for monitoring MySQL queries that we&#8217;ve used in the past on a couple of our boxes so I decided to fire up this AJAX version and see how it fared. AjaxMyTop received <a href="http://ajaxian.com/archives/779">3.7 out of 5 rating at Ajaxian</a> as of this writing.</p>
<p>Installation started at 8:38am PST.  gunzip ajaxMyTop.tar.gz. tar -xvf  ajaxMyTop.tar.  Threw dom errors right away, documentation is non-existent. Error was result of PHP 5.x required for the program. Turns out <a href="http://sourceforge.net/forum/forum.php?thread_id=1431063&#038;forum_id=526557">Michael Kimsal hacked up processlist.php</a> to work in PHP4. Here is the <a href="http://www.php-scripts.com/examples/processlist.phps">PHP 4 process.php source file</a> you&#8217;ll need if you aren&#8217;t running PHP5. And no, I didn&#8217;t clean up the indenting. Total time spent on this project: 20 minutes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20060131/78/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Query results within last X days</title>
		<link>http://www.php-scripts.com/20051005/31/</link>
		<comments>http://www.php-scripts.com/20051005/31/#comments</comments>
		<pubDate>Wed, 05 Oct 2005 14:10:05 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[PHP 4.x]]></category>

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

		<guid isPermaLink="false">http://www.php-scripts.com/?p=31</guid>
		<description><![CDATA[Sometimes it is necessary to query data from the last 7 days. Here&#8217;s how to create a dynamic query inside a script using PHP / MySQL to return a list of users who last visited within X number of days (7 by default):

&#60;?php
$now = time&#40;&#41;; // this is the current UNIX timestamp for the server
$days [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes it is necessary to query data from the last 7 days. Here&#8217;s how to create a dynamic query inside a script using PHP / MySQL to return a list of users who last visited within X number of days (7 by default):</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="re0">$now</span> = <a href="http://www.php.net/time"><span class="kw3">time</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// this is the current UNIX timestamp for the server</span><br />
<span class="re0">$days</span> = <span class="nu0">7</span>; <span class="co1">// change to number of days</span><br />
<span class="re0">$days_ago</span> = <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#8220;Y-m-d H:i:s&#8221;</span>, <span class="br0">&#40;</span><span class="re0">$now</span> - <span class="br0">&#40;</span><span class="nu0">86400</span> * <span class="re0">$days</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp;<span class="re0">$query</span> = <span class="st0">&#8220;SELECT username FROM users WHERE lastvisited &gt; &#8216;$days_ago&#8217;&#8221;</span>;<br />
&nbsp;<span class="re0">$result</span> = @<a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="re0">$query</span>,<span class="re0">$mysql_link</span><span class="br0">&#41;</span>;<br />
&nbsp;<span class="kw1">if</span><span class="br0">&#40;</span>@<a href="http://www.php.net/mysql_num_rows"><span class="kw3">mysql_num_rows</span></a><span class="br0">&#40;</span><span class="re0">$result</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span><span class="br0">&#40;</span><span class="re0">$row</span> = <a href="http://www.php.net/mysql_fetch_row"><span class="kw3">mysql_fetch_row</span></a><span class="br0">&#40;</span><span class="re0">$result</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;$row[0] &lt;br /&gt;&#8221;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp;<span class="br0">&#125;</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Notes:<br />
- $mysql_link contains the MySQL database connection link.<br />
- 86400 is the number of seconds in a day: 60 seconds x 60 minutes x 24 hours = 86,400</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20051005/31/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Turck MMCache and PHP encoder</title>
		<link>http://www.php-scripts.com/20050930/29/</link>
		<comments>http://www.php-scripts.com/20050930/29/#comments</comments>
		<pubDate>Fri, 30 Sep 2005 13:59:20 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Optimization]]></category>

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

		<category><![CDATA[PHP 4.x]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/?p=29</guid>
		<description><![CDATA[I&#8217;m not sure how current or updated this is because I found some broken project links (all links below work) but these projects seem promising and worth closer inspection, especially if one is looking to make their PHP scripts run faster.
Turck MMcache:
&#8230; a free open source PHP accelerator, optimizer, encoder and dynamic content cache for [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not sure how current or updated this is because I found some broken project links (all links below work) but these projects seem promising and worth closer inspection, especially if one is looking to make their PHP scripts run faster.</p>
<p><a href="http://turck-mmcache.sourceforge.net/index_old.html">Turck MMcache</a>:<br />
<blockquote>&#8230; a free open source PHP accelerator, optimizer, encoder and dynamic content cache for PHP. It increases performance of PHP scripts by caching them in compiled state, so that the overhead of compiling is almost completely eliminated. Also it uses some optimizations to speed up execution of PHP scripts. Turck MMCache typically reduces server load and increases the speed of your PHP code by 1-10 times.</p></blockquote>
<p>Jason Sheets created http://phpcoder.sourceforge.net/ - PHPCoder - which is a GUI front end to the Turck MMCache Encoder which allows encoding of PHP files and the following options: Expire scripts aftter a certain amount of time, restrict server IP, restrict server name and restrict visitor IP. PHP Coder is distributed under the <a href="http://www.opensource.org/licenses/bsd-license.php">BSD License</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20050930/29/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Parsing names and numbers out of text</title>
		<link>http://www.php-scripts.com/20050923/25/</link>
		<comments>http://www.php-scripts.com/20050923/25/#comments</comments>
		<pubDate>Sat, 24 Sep 2005 04:51:28 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Functions]]></category>

		<category><![CDATA[PHP 5.x]]></category>

		<category><![CDATA[PHP 4.x]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/?p=25</guid>
		<description><![CDATA[At one of our sites we needed to parse out some names and numbers from a log file. The strings also contained comment text and the order would be mixed format. These types of regular expression parsing can be tricky. 
Let&#8217;s take a look at the two possible formats of the strings:
FORMAT #1:
[00:02:18][jvastine]7.52..I agree with [...]]]></description>
			<content:encoded><![CDATA[<p>At one of our sites we needed to parse out some names and numbers from a log file. The strings also contained comment text and the order would be mixed format. These types of regular expression parsing can be tricky. </p>
<p>Let&#8217;s take a look at the two possible formats of the strings:</p>
<p>FORMAT #1:<br />
[00:02:18][jvastine]7.52..I agree with Rowdy, but they do have potential!</p>
<p>FORMAT #2:<br />
[00:02:18][@Forser]My vote is 5</p>
<p>Can you see a pattern for extracting the name and numbers from the string? Let&#8217;s whip up the code to parse these two types of strings as a test:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="re0">$text</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;[00:02:18][jvastine]7.52..I agree with Rowdy, but they do have potential!&#8217;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;[00:02:18][@Forser]My vote is 5&#8242;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>;<br />
<span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$text</span> <span class="kw1">as</span> <span class="re0">$string</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;&lt;hr&gt;testing: &#8220;</span> . <span class="re0">$string</span> . <span class="st0">&#8216;&lt;br /&gt;&#8217;</span>;<br />
&nbsp; <a href="http://www.php.net/preg_match"><span class="kw3">preg_match</span></a><span class="br0">&#40;</span><span class="st0">&#8220;/<span class="es0">\[</span>([<span class="es0">\@</span>a-z]+)<span class="es0">\]</span>([a-z ]+([0-9<span class="es0">\.</span>]+)|([0-9<span class="es0">\.</span>]+))/i&#8221;</span>,<span class="re0">$string</span>,<span class="re0">$matched</span><span class="br0">&#41;</span>;<br />
&nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;&lt;b&gt;$matched[1]&lt;/b&gt;&lt;br /&gt;&#8221;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$matched</span><span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span> != <span class="st0">&#8221;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a> <a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8220;%02f&#8221;</span>,<span class="re0">$matched</span><span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/print"><span class="kw3">print</span></a> <a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8220;%02f&#8221;</span>,<span class="re0">$matched</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="co1">// end for loop</span><br />
<span class="br0">&#125;</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Note the use of the sprintf() to format the number because it might be a decimal. We can trim the decimal if it&#8217;s all zeroes, but in some cases we may want to include the decimal. Also as one of my smart tech friends pointed out a string that contains multiple numbers could screw this up.</p>
<p>FORMAT #3:<br />
[00:02:18][jvastine]16 hot dogs&#8230;I vote is 7</p>
<p>Now how would we parse out the vote of 7 and ignore the 16? This one is a bit easier because we could analyze the numbers (by going to $match[4], $match[5], etc), since a valid rating is only 1-10 in the above example, but what if there was a format with multiple rating numbers?</p>
<p>FORMAT #4:<br />
[00:02:18][jvastine]6 hot dogs&#8230;I vote is 7</p>
<p>How to determine which is the vote in this case? This is a trick question. And not as easy one. Thoughts?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20050923/25/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Using header to redirect the browser</title>
		<link>http://www.php-scripts.com/20050922/24/</link>
		<comments>http://www.php-scripts.com/20050922/24/#comments</comments>
		<pubDate>Fri, 23 Sep 2005 02:16:31 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Functions]]></category>

		<category><![CDATA[PHP 5.x]]></category>

		<category><![CDATA[PHP 4.x]]></category>

		<category><![CDATA[PHP 3.x]]></category>

		<guid isPermaLink="false">http://www.php-scripts.com/?p=24</guid>
		<description><![CDATA[The PHP header function allows redirection of the browser, among other cool things. 
Example redirecting the browser to php-scripts.com homepage:

&#60;?php
header&#40;”Location: http://www.php-scripts.com/”);
?&#62;

]]></description>
			<content:encoded><![CDATA[<p>The PHP header function allows redirection of the browser, among other cool things. </p>
<p>Example redirecting the browser to php-scripts.com homepage:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<a href="http://www.php.net/header"><span class="kw3">header</span></a><span class="br0">&#40;</span>”Location: http:<span class="co1">//www.php-scripts.com/”);</span><br />
<span class="kw2">?&gt;</span></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20050922/24/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Using floor and time to calculate number of days elapsed from UNIX timestamp</title>
		<link>http://www.php-scripts.com/20050921/23/</link>
		<comments>http://www.php-scripts.com/20050921/23/#comments</comments>
		<pubDate>Wed, 21 Sep 2005 20:06:51 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<category><![CDATA[PHP 5.x]]></category>

		<category><![CDATA[PHP 4.x]]></category>

		<category><![CDATA[PHP 3.x]]></category>

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

		<guid isPermaLink="false">http://www.php-scripts.com/?p=23</guid>
		<description><![CDATA[Finding out the number of days elapsed from a UNIX timestamp is actually pretty straightforward. If you only know two dates then first you&#8217;ll need to convert the date to a UNIX timestamps and then subtract the most recent timestamp from the older timestamp like this:

&#60;?php
$old_timestamp = 1102971600;
$elapsed_seconds = time&#40;&#41; - $old_timestamp;
print &#8216;Seconds elapsed since [...]]]></description>
			<content:encoded><![CDATA[<p>Finding out the number of days elapsed from a UNIX timestamp is actually pretty straightforward. If you only know two dates then first you&#8217;ll need to convert the date to a UNIX timestamps and then subtract the most recent timestamp from the older timestamp like this:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="re0">$old_timestamp</span> = <span class="nu0">1102971600</span>;<br />
<span class="re0">$elapsed_seconds</span> = <a href="http://www.php.net/time"><span class="kw3">time</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span> - <span class="re0">$old_timestamp</span>;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8216;Seconds elapsed since &#8216;</span> . <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#8220;m-d-Y H:i:s&#8221;</span>,<span class="re0">$old_timestamp</span><span class="br0">&#41;</span> . <span class="st0">&#8220;: &lt;b&gt;$elapsed_seconds&#8221;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Now, let&#8217;s convert those seconds to days using the floor() function, by just adding the following code to the code above:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="coMULTI">/*<br />
divide #seconds by 60 for minutes<br />
divide #of minutes by 60 for hours<br />
divide #of hours by 24 for days<br />
use floor to get integer for day<br />
*/</span><br />
<span class="re0">$elapsed_days</span> = <a href="http://www.php.net/floor"><span class="kw3">floor</span></a><span class="br0">&#40;</span><span class="br0">&#40;</span><span class="re0">$elapsed_seconds</span> / <span class="nu0">60</span><span class="br0">&#41;</span> / <span class="nu0">60</span> / <span class="nu0">24</span><span class="br0">&#41;</span>; <br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;&lt;br /&gt;Number of days elapsed: $elapsed_days&#8221;</span>;</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20050921/23/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to use preg_replace to replace phrase between question marks</title>
		<link>http://www.php-scripts.com/20050920/14/</link>
		<comments>http://www.php-scripts.com/20050920/14/#comments</comments>
		<pubDate>Tue, 20 Sep 2005 18:03:52 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[PHP 5.x]]></category>

		<category><![CDATA[PHP 4.x]]></category>

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

		<guid isPermaLink="false">http://www.php-scripts.com/?p=14</guid>
		<description><![CDATA[
&#60;?php
$string = “?This is a test that should be in single quotes quotes?”;
$text = preg_replace&#40;”/&#40;\?&#40;&#91;^\?&#93;+&#41;\?&#41;/”,”‘\\2′”,$string&#41;;
?&#62;

regular expression produces:
This is a test that should be in single quotes’
]]></description>
			<content:encoded><![CDATA[<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="re0">$string</span> = “?This is a test that should be in single quotes quotes?”;<br />
<span class="re0">$text</span> = <a href="http://www.php.net/preg_replace"><span class="kw3">preg_replace</span></a><span class="br0">&#40;</span>”/<span class="br0">&#40;</span>\?<span class="br0">&#40;</span><span class="br0">&#91;</span>^\?<span class="br0">&#93;</span>+<span class="br0">&#41;</span>\?<span class="br0">&#41;</span>/”,”‘\\<span class="nu0">2</span>′”,<span class="re0">$string</span><span class="br0">&#41;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p>regular expression produces:</p>
<p>This is a test that should be in single quotes’</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20050920/14/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to make an anniversary counter announcement script using ordinal numbers</title>
		<link>http://www.php-scripts.com/20050919/22/</link>
		<comments>http://www.php-scripts.com/20050919/22/#comments</comments>
		<pubDate>Mon, 19 Sep 2005 20:01:55 +0000</pubDate>
		<dc:creator>TDavid</dc:creator>
		
		<category><![CDATA[PHP 5.x]]></category>

		<category><![CDATA[PHP 4.x]]></category>

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

		<guid isPermaLink="false">http://www.php-scripts.com/?p=22</guid>
		<description><![CDATA[Today being it is our 16th wedding anniversary I decided it would be appropriate to build a small script to keep track and post a message automatically on our special day together. I wanted this program to remember what year of our anniversary it was without me having to remind it: a great use for [...]]]></description>
			<content:encoded><![CDATA[<p>Today being it is our 16th wedding anniversary I decided it would be appropriate to build a small script to keep track and post a message automatically on our special day together. I wanted this program to remember what year of our anniversary it was without me having to remind it: a great use for a variable.</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="re0">$anniversary_date</span> = <span class="st0">&#8216;0919&#8242;</span>;<br />
<span class="re0">$start_year</span> = <span class="nu0">1989</span>;<br />
<span class="re0">$the_suffix</span> = <span class="st0">&#8216;th&#8217;</span>;<br />
<span class="re0">$number_years</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;Y&#8221;</span><span class="br0">&#41;</span> - <span class="re0">$start_year</span><span class="br0">&#41;</span> . <span class="re0">$the_suffix</span>;<br />
<span class="kw1">if</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="re0">$anniversary_date</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp;<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;Happy $number_years&nbsp; anniversary, Kara!&#8221;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Something worth noting is 16th, 17th, 18th, 19th and 20th will work with the $the_suffix &#8230; but what about 21st, 22nd, 23rd? Do we need to add filtering or does the date PHP function offer something to automatically provide this for us?</p>
<p>In fact it does! It&#8217;s called the &#8216;S&#8217; suffix and works good as a date parameter for dealing with this situation.  For example:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;today is the &#8220;</span> . <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#8220;dS&#8221;</span><span class="br0">&#41;</span> . <span class="st0">&#8216;of the month.&#8217;</span>; <span class="co1">// adds st, nd, rd, th</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p>This will output the proper 20th, 21st, 22nd, 23rd, 24th, etc format.</p>
<p>Now how can this be made to work on <i>any number</i>, not just a date because I&#8217;m optimistic that we&#8217;ll be together beyond 31 years. One solution is to create your own function, as robf did and posted to Zend in the way of an <a href="http://www.zend.com/codex.php?id=881&#038;single=1">ordinal($number) function</a>. </p>
<p>I remained curious, though if we could fake a date in years, and then strip the first part of the years off to cover any possible usage of anniversary numbers and not have to resort to using our own function? We can do this up until our 69th annivesary using mktime(), date and substr() functions using the following code:</p>
<div class="codesnip-container" >
<div class="codesnip">&lt;?php<br />
<span class="re0">$elapsed</span> = <a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&#8220;Y&#8221;</span><span class="br0">&#41;</span> - <span class="re0">$start_year</span> + <span class="nu0">2001</span>; <span class="co1">// add 2001</span><br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a> <span class="st0">&#8220;&lt;br /&gt;Today ($elapsed) is our &#8220;</span> . <a href="http://www.php.net/substr"><span class="kw3">substr</span></a><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;YS&#8221;</span>,<a href="http://www.php.net/mktime"><span class="kw3">mktime</span></a><span class="br0">&#40;</span><span class="nu0">0</span>,<span class="nu0">0</span>,<span class="nu0">0</span>,<span class="nu0">0</span>,<span class="nu0">0</span>,<span class="re0">$elapsed</span><span class="br0">&#41;</span><span class="br0">&#41;</span>,<span class="nu0">2</span>,<span class="nu0">4</span><span class="br0">&#41;</span>&nbsp; . <span class="st0">&#8216; anniversary.&#8217;</span>; <span class="co1">//</span><br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Definitely not the most practical code on the planet, nor anything I&#8217;d ever use beyond just playing around aloud here, but I was trying to stretch my brain for other possible ways to do this using built-in PHP functions. </p>
<p>Have I missed a built-in PHP ordinal number function that works like the S suffix in date()?</p>
<p>I&#8217;m sure my wife would rather get flowers than flowery code. I better get on that!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.php-scripts.com/20050919/22/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

