<?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>The Fruits of my Labour &#187; PHP</title>
	<atom:link href="http://www.toao.net/web-programming/php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.toao.net</link>
	<description>by Mango</description>
	<lastBuildDate>Sun, 18 Jul 2010 21:44:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>12 Things Mango Wishes He&#039;d Known When He Was a PHP N00b.</title>
		<link>http://www.toao.net/266-essential-php-tips</link>
		<comments>http://www.toao.net/266-essential-php-tips#comments</comments>
		<pubDate>Mon, 29 Jun 2009 16:32:15 +0000</pubDate>
		<dc:creator>Mango</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.toao.net/?p=266</guid>
		<description><![CDATA[Never use Register Globals.&#160; Ever.&#160; And if you ever even THINK of using Register Globals, and we find out, Mango is going to send someone over to your house to kick your ass.&#160; Are we clear? As much as we love working with PHP, Register Globals is something that should be filed under "Monumentally Bad [...]]]></description>
			<content:encoded><![CDATA[<br /><ol style='font-weight:bold;'>
<li>Never use Register Globals.&nbsp; Ever.&nbsp; And if you ever even THINK of using Register Globals, and we find out, Mango is going to send someone over to your house to kick your ass.&nbsp; Are we clear?
<div style='font-weight:normal;'>
As much as we love working with PHP, Register Globals is something that should be filed under "Monumentally Bad Ideas".&nbsp; And unfortunately, it's something that, at first glance, appears convenient.&nbsp; If a user submits a form to a PHP script with Register Globals enabled, the script will create a variable for each form element.&nbsp; However, keep in mind that this allows your end users to set any variable in your script that they like.&nbsp; Here is a common mistake that we see:<br />


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$logged_in</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> secret_functions_allow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div></div>
In a situation like this, the author has first run some code to see if the user is logged in or not.&nbsp; If so, the script will have defined the $logged_in variable.&nbsp; When the above line of code fires, it will provide the user with some functions only availble to authenticated users.&nbsp; Innocent enough, yes?&nbsp; However, to "hack" this, all that a malicious user would need to do is form a URL like http://www.example.com/?logged_in=1.&nbsp; To turn off Register Globals, add <b>php_flag register_globals off</b> to .htaccess or <b>register_globals = Off</b> to php.ini.&nbsp; Many administrators ban use of Register Globals entirely, so by developing without it, your script will be more portable.&nbsp; This is a good thing.</div></li>
</ol><br  />
<span id="more-266"></span><br   />

<ol start='2' style='font-weight:bold;'>
<li>Turn notices on when developing code.
<div style='font-weight:normal;'>
Yes, we know they're annoying.&nbsp; However, notices provide you with some valuable information you can use both to improve your script and improve your skills as a programmer.&nbsp; One thing that notices will alert you to is undefined variables.&nbsp; Continuing with the above example, the fictitous script assumes that the $logged_in variable will be defined if the user is logged in.&nbsp; Proper programming practice would be a scenario more like $logged_in==1 if the user is logged in and $logged_in==0 if not.&nbsp; If notices are turned on, PHP will alert you to this.&nbsp; To turn notices on, add <b>php_value display_errors on</b> and <b>php_value error_reporting 2147483647</b> to .htaccess or <b>display_errors = On</b> and <b>error_reporting = E_ALL</b> to php.ini.&nbsp; You can also use <b>ini_set('display_errors', 1); error_reporting(E_ALL);</b> in your PHP script however this will not display parse errors such as a missing quote or bracket.<br  />
<b>Security note:</b> you should turn display_errors off for a production server.&nbsp; It's best to use it only when developing code.</div>
</li>

<li>Don't rely on Magic Quotes.
<div style='font-weight:normal;'>
Magic Quotes is a feature of PHP before (but not including) version 6.&nbsp; It was originally created to help prevent <a href='http://php.net/manual/en/security.database.sql-injection.php' target='_blank'>SQL Injection</a> however new thinking is that programmers should write secure code (!) rather than rely on Magic Quotes.&nbsp; Here is an example of a function that you may pass $_GET, $_POST, and $_COOKIE to.&nbsp; It will work whether or not Magic Quotes is enabled.<br />


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> mysql_real_escape_array<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #990000;">unset</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">get_magic_quotes_gpc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> mysql_real_escape_array<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #990000;">unset</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">get_magic_quotes_gpc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
   <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>
</div>

</li>
<li>Do not use the path to your files in your script.
<div style='font-weight:normal;'>
There's nothing worse than having to move servers in a hurry, realizing that you now have a new path to your home directory, and having to change a hardcoded path in dozens of files.&nbsp; Instead of hardcoding paths, use $_SERVER['DOCUMENT_ROOT'] to have your script discover your document root.&nbsp; Or, use dirname(__FILE__) to find the directory that the script is located in.&nbsp; (Note that this is literally dirname(__FILE__) - you don't replace __FILE__ with anything.)  Or, use the <a href='http://php.net/getcwd' target='_blank'>getcwd</a> function.</div>

</li>
<li>Avoid having the same or even similar code in different parts of your script.
<div style='font-weight:normal;'>
You want to do this simply because it's more efficient.&nbsp; It's faster to write code when you don't have to write the same code over and over, it's faster to modify code when you don't have to modify it in multiple places, and it's easier for other people to read the finished product when the code is short.&nbsp; If you have to do the same thing more than once in your script, use functions or loops.&nbsp; If you have to do the same thing in multiple scripts, use include files.</div>

</li>
<li>Don't do <i>for ($i = 0; $i < count($array); $i++) {</i><br   />
<div style='font-weight:normal;'>
Yes, this will run and will often produce the desired results.&nbsp; However, the <a href='http://php.net/count' target='_blank'>count</a> function will run at every single iteration.&nbsp; Instead, it's faster to do:<br />


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span><span style="color: #339933;">=</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$count</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></pre></div></div>
</div>

</li>
<li>Come up with a naming scheme for functions, variables, and column names.
<div style='font-weight:normal;'>
Someone once told us that functions and variables should all be named with as much care as one names their first child.&nbsp; We agree.&nbsp; If your naming scheme is effective, one should be able to <b>guess</b> names by looking at other names.&nbsp; The <a href='http://php.net/manual/en/book.mailparse.php' target='_blank'>Mailparse Extension</a> is a good example of effective naming schemes.&nbsp; Additionally, take care when defining the order of parameters in a function.&nbsp; One example of why this is a good idea is the <a href='http://php.net/strpos' target='_blank'>strpos</a> function and the <a href='http://php.net/str_replace' target='_blank'>str_replace</a> function.&nbsp; These functions have two strikes against them.&nbsp; By looking at str_replace, I would assume strpos would be named str_pos.&nbsp; Additionally, the parameters are in different orders: strpos requires the subject first and str_replace requires the subject last.<br />
<br />
For naming functions, we separate words with underscores.&nbsp; The first word in the name is a word that defines the general group of functions that we're writing, (for ease of identifying what the function is for, and also for ease of sorting when writing documentation) and then use more words as necessary to describe the function.&nbsp; Similar functions should have parameters in the same order, with parameters that have default values at the end.&nbsp; For naming database column names, we often use the actual human readable phrase, converted to lower case, with underscores instead of spaces.&nbsp; This occasionally makes for a slightly long column name, but the advantage is that we can later use str_replace and <a href='http://php.net/ucwords' target='_blank'>ucwords</a> to display the name and not need to cross-reference a list of human-readable column names.</div>

</li>
<li>Use UTF-8
<div style='font-weight:normal;'>
There's a great article about <a href='http://developer.loftdigital.com/blog/php-utf-8-cheatsheet' target='_blank'>using UTF-8 with PHP and MySQL</a> at developer.loftdigital.com.</div>

</li>
<li>Use SELECT * only when necessary.
<div style='font-weight:normal;'>
This is a MySQL tip, however we include it here because a great deal of PHP students also wish to learn MySQL.&nbsp; SELECT * is not inherently a bad thing to do, however it's often misused.&nbsp; The only time that it should be used is when you need to select <i>every single column</i> in a table <b>and</b> <i>you do not know in advance what columns the table will have.</i>  We see many people selecting all columns in the table but only actually using one or two.&nbsp; In this case, it's better to do something such as <b>SELECT `first_name`,`last_name`</b>.&nbsp; This makes for a longer query, however since the entire row is not loaded, your script uses less memory.<br />
<br />
Here's an example of where you DO want to use SELECT *.&nbsp; Let's say you have a program that displays information about customers from a database.&nbsp; Perhaps one day you decide that you want to keep track of customers' birthdays.&nbsp; So you add a column to your MySQL table named `birthday`.&nbsp; When your program displays the information about the customer, it is necessary to see all of the information.&nbsp; Because in this case columns such as the `birthday` column may be added in the future and we do not yet know what columns may be added, we have satisfied both of the requirements for using SELECT *.</div>

</li>
<li>Make a wrapper for MySQL queries.
<div style='font-weight:normal;'>
This might not be necessary for quick scripts but it's something we like to do for large projects.&nbsp; The simplest wrapper is as such:<br />


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> mysql_mangos_query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div></div>
There are many advantages to this technique.&nbsp; For example, one may want to use the mysql_error() function to see if the query returned an error, and if so, write the error to a log.&nbsp; Or, if one is working on a large project with multiple tables, one might want to prefix the tables with something like projectname1_ so that multiple instances of the project may run with different data.&nbsp; One example of this is the content management system Joomla.&nbsp; All of Joomla's queries look like <b>SELECT [columns] FROM #__content WHERE...</b>  Joomla removes #__ and replaces it with the appropriate table prefix.</div>

</li>
<li>Learn how to use regular expressions.
<div style='font-weight:normal;'>
Regular expressions are by far the most powerful underused feature of PHP that we see.&nbsp; Students often avoid using them because they look intimidating.&nbsp; We agree, however they're easy to get to know in 15 or 20 minutes.&nbsp; They're often a very simple and efficient solution to data manipulation problems.&nbsp; If you're a programming student, one can easily impress friends (well, fellow programming students anyway) by being able to read and write complex regular expressions.</div>

</li>
<li>Finally, know where to go for documentation.
<div style='font-weight:normal;'>
It's always a good idea to have documentation at your fingertips.&nbsp; We certainly don't claim to know every PHP function there is to know (although that would be really cool).&nbsp; To find documentation on any function, simply go to http://php.net/functionname.&nbsp; If you don't know the name of the function you need, look up a similar function and see if the one you want is listed as a related function.&nbsp; Often, you will also find examples and tips on usage.&nbsp; These are very helpful.&nbsp; For example, someone wanting to find out if a string is contained within a string might look up preg_match.&nbsp; However, this operation does not actually require regular expressions, and as the manual will tell you, it is faster in this situation to use strpos instead.</div></li>
</ol>
Happy coding!]]></content:encoded>
			<wfw:commentRss>http://www.toao.net/266-essential-php-tips/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Mango&#039;s PHP PostScript Functions</title>
		<link>http://www.toao.net/244-mangos-php-postscript-functions</link>
		<comments>http://www.toao.net/244-mangos-php-postscript-functions#comments</comments>
		<pubDate>Thu, 18 Jun 2009 04:59:45 +0000</pubDate>
		<dc:creator>Mango</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.toao.net/?p=244</guid>
		<description><![CDATA[The latest techniques we've been playing with involve using a PHP script on our server in Michigan to print to a remote printer at our office in Vancouver.&#160; In the past we've done this by generating an XHTML document with high-resolution images and simply prompting the user to print it.&#160; This worked, but if any [...]]]></description>
			<content:encoded><![CDATA[<br />The latest techniques we've been playing with involve using a PHP script on our server in Michigan to print to a remote printer at our office in Vancouver.&nbsp; In the past we've done this by generating an <a href='/10-600dpi-on-a-website-yes-its-possible'>XHTML document with high-resolution images</a> and simply prompting the user to print it.&nbsp; This worked, but if any sort of precision was required, the user had to configure their browser's page setup just so, and to make things even more difficult, different browsers required different settings.<br />
<br />
Enter PostScript, a language understood by many laser printers.&nbsp; With PostScript, we can go directly from our script to the printer, (pipe the finished PostScript document to the printer on port 9100) eliminating the stop off at the browser.&nbsp; And, we can position things on our page with as much precision as necessary.<br />
<br  />
<span id="more-244"></span><br   />
<br />
PHP has a basic set of functions for creating PostScript documents - and we're very grateful for this because it meant we didn't have to learn PostScript itself - but it lacks advanced text handling features such as vertical alignment and automatic resizing of text.&nbsp; And, while it can do justified text, it does it in very mysterious ways.&nbsp; Fortunately, these issues were not a problem for Mango, and Mango's PostScript Functions were born.<br />
<br />
Here's the source to <a href='/pub/PostScript/mangos_ps_functions.txt'>Mango's PostScript Functions including ps_super_boxed</a>, a <a href='/pub/PostScript/mangos_ps_demo.txt'>demo of how to vertically align and dynamically resize text with PHP and pslib</a>, and the <a href='/pub/PostScript/mangos_ps_demo.pdf'>results of the demo</a>, converted to PDF for easy viewing.&nbsp; (One note: the demo requires the font file Helvetica.afm and we're not sure if we're allowed to distribute that, but it's very easy to find with Google.)<br />
<br />
Enjoy!]]></content:encoded>
			<wfw:commentRss>http://www.toao.net/244-mangos-php-postscript-functions/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Replacing smart quotes, em-dashes, and ellipses with MySQL or PHP</title>
		<link>http://www.toao.net/48-replacing-smart-quotes-and-em-dashes-in-mysql</link>
		<comments>http://www.toao.net/48-replacing-smart-quotes-and-em-dashes-in-mysql#comments</comments>
		<pubDate>Thu, 05 Mar 2009 21:28:24 +0000</pubDate>
		<dc:creator>Mango</dc:creator>
				<category><![CDATA[Joomla]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.toao.net/48/replacing-smart-quotes-and-em-dashes-in-mysql/</guid>
		<description><![CDATA[Alternate title: "Help!&#160; My Quotes Appear as Question Marks or Other Strange Characters!" The "Smart quotes" feature in Microsoft Office transforms straight quotes into curly quotes.&#160; It also transforms hyphens into em-dashes and three periods into ellipses.&#160; While one might think, "How lovely!&#160; My document looks almost as if I'm educated!" readers of said document [...]]]></description>
			<content:encoded><![CDATA[<br />Alternate title: "Help!&nbsp; My Quotes Appear as Question Marks or Other Strange Characters!"<br />
<br />
The "Smart quotes" feature in Microsoft Office transforms straight quotes into curly quotes.&nbsp; It also transforms hyphens into em-dashes and three periods into ellipses.&nbsp; While one might think, "How lovely!&nbsp; My document looks almost as if I'm educated!" readers of said document may not.&nbsp; Microsoft, in its infinite wisdom, decided to assign special characters such as the ones I just mentioned to a range of codes above 128.&nbsp; Problem: these codes were already assigned to other characters, resulting in frustrating incompatibility with non-Microsoft systems.<br />
<br />
Keep reading for some PHP and MySQL code to help out with this issue, as well as a Joomla! plugin.<br />
<br  />
<span id="more-48"></span><br   />
<br />
Our introduction to this was in a situation where we had people using many different systems submitting articles to one of our programs.&nbsp; We decided that we wanted all our articles to use straight quotes, hyphens, and periods.&nbsp; This was partly for consistency, and partly because these characters are common to many character sets and won't cause incompatibilities.<br />
<br />
This article isn't really intended as a complete explanation of charsets, though we recommend <a href="http://www.joelonsoftware.com/articles/Unicode.html" target='_blank'>The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets</a> if you're interested in learning more.&nbsp; We will however show you some MySQL and PHP techniques for replacing all instances of smart quotes, plus the en dash, em dash, and ellipsis with straight quotes, one or two dashes, or three dots.&nbsp; This code should operate with both the Windows-1252 charset, and also UTF-8, an encoding with an extended character set that has made it the preferred encoding for email and websites.<br />
<br />
MySQL:<br />


<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># First, replace UTF-8 characters.</span>
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`t`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`c`</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`c`</span><span style="color: #66cc66;">,</span> 0xE28098<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;'&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`t`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`c`</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`c`</span><span style="color: #66cc66;">,</span> 0xE28099<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;'&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`t`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`c`</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`c`</span><span style="color: #66cc66;">,</span> 0xE2809C<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'&quot;'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`t`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`c`</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`c`</span><span style="color: #66cc66;">,</span> 0xE2809D<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'&quot;'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`t`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`c`</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`c`</span><span style="color: #66cc66;">,</span> 0xE28093<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'-'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`t`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`c`</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`c`</span><span style="color: #66cc66;">,</span> 0xE28094<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'--'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`t`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`c`</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`c`</span><span style="color: #66cc66;">,</span> 0xE280A6<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'...'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;"># Next, replace their Windows-1252 equivalents.</span>
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`t`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`c`</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`c`</span><span style="color: #66cc66;">,</span> char<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">145</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;'&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`t`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`c`</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`c`</span><span style="color: #66cc66;">,</span> char<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">146</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;'&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`t`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`c`</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`c`</span><span style="color: #66cc66;">,</span> char<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">147</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'&quot;'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`t`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`c`</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`c`</span><span style="color: #66cc66;">,</span> char<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">148</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'&quot;'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`t`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`c`</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`c`</span><span style="color: #66cc66;">,</span> char<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">150</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'-'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`t`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`c`</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`c`</span><span style="color: #66cc66;">,</span> char<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">151</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'--'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`t`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`c`</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`c`</span><span style="color: #66cc66;">,</span> char<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">133</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'...'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>
PHP:<br />


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// First, replace UTF-8 characters.</span>
<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span>
 <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\xe2</span><span style="color: #660099; font-weight: bold;">\x80</span><span style="color: #660099; font-weight: bold;">\x98</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\xe2</span><span style="color: #660099; font-weight: bold;">\x80</span><span style="color: #660099; font-weight: bold;">\x99</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\xe2</span><span style="color: #660099; font-weight: bold;">\x80</span><span style="color: #660099; font-weight: bold;">\x9c</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\xe2</span><span style="color: #660099; font-weight: bold;">\x80</span><span style="color: #660099; font-weight: bold;">\x9d</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\xe2</span><span style="color: #660099; font-weight: bold;">\x80</span><span style="color: #660099; font-weight: bold;">\x93</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\xe2</span><span style="color: #660099; font-weight: bold;">\x80</span><span style="color: #660099; font-weight: bold;">\x94</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\xe2</span><span style="color: #660099; font-weight: bold;">\x80</span><span style="color: #660099; font-weight: bold;">\xa6</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
 <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'-'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'--'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'...'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
 <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Next, replace their Windows-1252 equivalents.</span>
 <span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span>
 <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">145</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">146</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">147</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">148</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">150</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">151</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">133</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
 <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'-'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'--'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'...'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
 <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>
Joomla!:<br  />
<a href='/pub/plg_StripSmartQuotes.zip'>Joomla! plugin for removing smart/curly quotes, em-dashes and ellipses.</a><br   />
<br />
<br />
Additionally, here's a table of character codes that you may find useful:<br />

<table border="0" cellpadding="2" cellspacing="0" style='width:100%;'>
<tr>
<th>Character</th>
<th>HTML Code</th>
<th>Windows</th>
<th>UTF-8</th>
<th>name</th>
</tr>
<tr>
<td>&lsquo;</td>
<td>&amp;lsquo;</td>
<td>145</td>
<td>E28098</td>
<td>left single curly quote</td>
</tr>
<tr>
<td>&rsquo;</td>
<td>&amp;rsquo;</td>
<td>146</td>
<td>E28099</td>
<td>right single curly quote</td>
</tr>
<tr>
<td>&ldquo;</td>
<td>&amp;ldquo;</td>
<td>147</td>
<td>E2809C</td>
<td>left double curly quote</td>
</tr>
<tr>
<td>&rdquo;</td>
<td>&amp;rdquo;</td>
<td>148</td>
<td>E2809D</td>
<td>right double curly quote</td>
</tr>
<tr>
<td>&ndash;</td>
<td>&amp;ndash;</td>
<td>150</td>
<td>E28093</td>
<td>en dash</td>
</tr>
<tr>
<td>&mdash;</td>
<td>&amp;mdash;</td>
<td>151</td>
<td>E28094</td>
<td>em dash</td>
</tr>
<tr>
<td>&hellip;</td>
<td>&amp;hellip;</td>
<td>133</td>
<td>E280A6</td>
<td>ellipsis</td>
</tr>
</table>
<br />
Further reading from Wikipedia:<br  />
<a href="http://en.wikipedia.org/wiki/UTF-8" target='_blank'>UTF-8</a><br   />
<br  />
<a href="http://en.wikipedia.org/wiki/ISO/IEC_8859-1" target='_blank'>ISO/IEC 8859-1</a><br   />
<br  />
<a href="http://en.wikipedia.org/wiki/Windows-1252" target='_blank'>Windows-1252</a>]]></content:encoded>
			<wfw:commentRss>http://www.toao.net/48-replacing-smart-quotes-and-em-dashes-in-mysql/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Some nifty PHP date/time functions</title>
		<link>http://www.toao.net/36-php-date-time-functions</link>
		<comments>http://www.toao.net/36-php-date-time-functions#comments</comments>
		<pubDate>Sat, 08 Mar 2008 20:11:23 +0000</pubDate>
		<dc:creator>Mango</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.toao.net/36/testing-the-new-code-highlighter/</guid>
		<description><![CDATA[I wrote two date/time functions in PHP that I was really pleased with so thought I'd post them. string friendlydate ( numeric or string $input ) Return value is a day in relation to today in the format "Today", "Yesterday", "Wednesday", "March 2", or "November 7, 2007". string howlong ( numeric or string $a, numeric [...]]]></description>
			<content:encoded><![CDATA[<br />I wrote two date/time functions in PHP that I was really pleased with so thought I'd post them.<br />
<br  />
<ul style='padding-top:0;margin-top:0;'>
<li><em>string <strong>friendlydate</strong> ( numeric or string $input )</em><br   />
Return value is a day in relation to today in the format "Today", "Yesterday", "Wednesday", "March 2", or "November 7, 2007".
</li>
<li><em>string <strong>howlong</strong> ( numeric or string $a, numeric or string $b )
</em>Return value is the <u>approximate</u> length of time between two dates, $a and $b.&nbsp; Examples are "4 seconds ago", "1 minute from now", "1 hour ago", "yesterday", "2 months ago", "1 year ago".</li>
</ul><br  />
<span id="more-36"></span><br   />


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> friendlydate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$input</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ymd&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ymd&quot;</span><span style="color: #339933;">,</span> hometime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Today&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ymd&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ymd&quot;</span><span style="color: #339933;">,</span> hometime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">24</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Yesterday&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span> <span style="color: #339933;">&gt;</span> hometime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">24</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;l&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y&quot;</span><span style="color: #339933;">,</span> hometime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;F jS&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;F jS, Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> howlong<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> hometime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> hometime<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;at the same time&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$aa</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$aa</span><span style="color: #339933;">;</span> <span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;away&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ago&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
 <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span><span style="color: #339933;">-</span><span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;1 second <span style="color: #006699; font-weight: bold;">$q</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">60</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$value</span> seconds <span style="color: #006699; font-weight: bold;">$q</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$value</span> <span style="color: #339933;">/=</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;1 minute <span style="color: #006699; font-weight: bold;">$q</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">60</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; minutes <span style="color: #006699; font-weight: bold;">$q</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$value</span> <span style="color: #339933;">/=</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;1 hour <span style="color: #006699; font-weight: bold;">$q</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">24</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; hours <span style="color: #006699; font-weight: bold;">$q</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$value</span> <span style="color: #339933;">/=</span> <span style="color: #cc66cc;">24</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;ago&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;yesterday&quot;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;tomorrow&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">31</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; days <span style="color: #006699; font-weight: bold;">$q</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$value</span> <span style="color: #339933;">/=</span> <span style="color: #cc66cc;">31</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;1 month <span style="color: #006699; font-weight: bold;">$q</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; months <span style="color: #006699; font-weight: bold;">$q</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$value</span> <span style="color: #339933;">/=</span> <span style="color: #cc66cc;">12</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;1 year <span style="color: #006699; font-weight: bold;">$q</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">return</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; years <span style="color: #006699; font-weight: bold;">$q</span>&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.toao.net/36-php-date-time-functions/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
