<?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>Martin's Blog &#187; PHP</title>
	<atom:link href="http://www.martienus.com/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.martienus.com</link>
	<description>Logic to Art</description>
	<lastBuildDate>Fri, 12 Feb 2010 19:07:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Check size constraints for multidimensional arrays</title>
		<link>http://www.martienus.com/code/check-size-constraints-for-multidimensional-arrays.html</link>
		<comments>http://www.martienus.com/code/check-size-constraints-for-multidimensional-arrays.html#comments</comments>
		<pubDate>Mon, 08 Feb 2010 16:17:59 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[constraints]]></category>
		<category><![CDATA[multidimensional]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[recursion]]></category>

		<guid isPermaLink="false">http://www.martienus.com/?p=57</guid>
		<description><![CDATA[Multidimensional arrays are commonly used as a complex data structure. Moreover, they are used to convey data between software systems internally. In some programming languages however, there is no way to constrain the size of arrays at any stage of the array&#8217;s lifetime. PHP for instance has no ability to do this. Therefore a way [...]]]></description>
			<content:encoded><![CDATA[<p>Multidimensional arrays are commonly used as a complex data structure. Moreover, they are used to convey data between software systems internally. In some programming languages however, there is no way to constrain the size of arrays at any stage of the array&#8217;s lifetime. PHP for instance has no ability to do this. Therefore a way must be devised to make sure the structure of the passed data conforms to what the receiving system expects.</p>
<p>Think for instance pixel <a href="http://docs.gimp.org/nl/plug-in-convmatrix.html">convolution matrices</a>. These are usually 3 by 3.</p>
<p>Check out my recursive PHP function to check sizes at any dimensional depth possible:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> isDimension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dimensions</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$amount</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dimensions</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$amount</span> <span style="color: #339933;">===</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">||</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$amount</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;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dimensions</span><span style="color: #009900;">&#41;</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;">$arr</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$element</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;">$element</span><span style="color: #009900;">&#41;</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>isDimension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$element</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dimensions</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
               <span style="color: #009900;">&#123;</span>
                  <span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
               <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">else</span>
            <span style="color: #009900;">&#123;</span>
               <span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
         <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #b1b100;">else</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And when we utilize this function in the following way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$subject</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</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: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</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: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>isDimension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Yay&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: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Nay&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The output will be:</p>
<pre>Yay
</pre>
<p>If at a certain dimension size does not matter you can skip it by entering -1 as the expected size:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$subject</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</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: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</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: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span>
			<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$wellIsIt</span> <span style="color: #339933;">=</span> isDimension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Returns true</span>
<span style="color: #000088;">$wellIsIt</span> <span style="color: #339933;">=</span> isDimension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Returns true</span>
<span style="color: #000088;">$wellIsIt</span> <span style="color: #339933;">=</span> isDimension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Returns false</span></pre></td></tr></table></div>

<p>This is a recursive function, meaning that it calls itself from within itself until a certain condition is met. Then it cascades the result back to the initial function call which in turn passes it back to whatever script called the function. More about this can be read <a href="http://en.wikipedia.org/wiki/Recursion_%28computer_science%29">on wikipedia</a>.</p>
<p>There ya go</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martienus.com/code/check-size-constraints-for-multidimensional-arrays.html/feed</wfw:commentRss>
		<slash:comments>8670</slash:comments>
		</item>
		<item>
		<title>PHP: Image Resizer Class</title>
		<link>http://www.martienus.com/code/php-image-resizer-class.html</link>
		<comments>http://www.martienus.com/code/php-image-resizer-class.html#comments</comments>
		<pubDate>Thu, 19 Jun 2008 16:43:46 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[resize]]></category>

		<guid isPermaLink="false">http://www.martienus.com/?p=15</guid>
		<description><![CDATA[Being able to resize images automatically is a must for websites where users can upload their own images, avatars and so on. In PHP we can do this with the GD library. We don&#8217;t want users to upload pictures that are 2000 pixels wide and 1 pixel high. This will disrupt the layout of the [...]]]></description>
			<content:encoded><![CDATA[<p>Being able to resize images automatically is a must for websites where users can upload their own images, avatars and so on. In PHP we can do this with the <a href="http://www.php.net/manual/en/book.image.php" target="_blank">GD library</a>. We don&#8217;t want users to upload pictures that are 2000 pixels wide and 1 pixel high. This will disrupt the layout of the site on which they are shown.</p>
<p>Thus we want the image to be resized to sane proportions and dimensions so that we can be sure they fit nicely into our websites&#8217; layout.</p>
<p>Therefore I made an image resizing class myself. It actually resamples the images for better results. Here are some of the features:</p>
<ul>
<li>Handles JPG, GIF, PNG and BMP image files</li>
<li>Can make multiple resized copies</li>
<li>Can simply duplicate the original</li>
<li>Can crop images to a certain aspect ratio, maintaining proportions</li>
<li>Can save to JPG, GIF, PNG and BMP</li>
<li>Can return the image as a string (for i.e. database storage)</li>
<li>Can return the image to the browser</li>
<li>Throws exceptions for unexpected behaviour</li>
</ul>
<p>Here is some code that shows how to utilize the class in your PHP application:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;imageresizer.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Increase the allowed memory size for the bigger images</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mem_size'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">32000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
try <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> imageResizer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'original.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Make a smaller version of the original</span>
	<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resize</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">400</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">300</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">400</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'result.gif'</span><span style="color: #339933;">,</span> GIF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Make a thumbnail of the original</span>
	<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resize</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'thumb_result.png'</span><span style="color: #339933;">,</span> PNG<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Retrieve the thumbnail as a string as BMP and show it in the browser as JPG</span>
	<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getString</span><span style="color: #009900;">&#40;</span>BMP<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">show</span><span style="color: #009900;">&#40;</span>JPG<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
catch<span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Catch and display any exceptional behaviour</span>
	<span style="color: #b1b100;">print</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Destroy object (executes the destructor) and more importantly, frees up memory</span>
<span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The resize method can have four values passed to it, namely: maximum width, maximum height, minimum width and minimum height. Either the width or the height should be the same when you use this functionality. Suppose we have the width at 400 pixels. The height of the result should be between 300 and 100 pixels. If we can&#8217;t fit the original in this aspect ratio constraint it will crop off the edges until it fits. This way the image is not stretched or compressed and we have control over both proportions and dimensions.</p>
<p><a href="http://www.martienus.com/wp-content/uploads/imageresizer.txt" target="_blank">Download the imageResizer class here!</a></p>
<p>If you come up with any improvements or bugs please let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martienus.com/code/php-image-resizer-class.html/feed</wfw:commentRss>
		<slash:comments>8463</slash:comments>
		</item>
		<item>
		<title>Convert Magic eDeveloper / Pervasive date: `Days since AD` in PHP</title>
		<link>http://www.martienus.com/code/convert-magic-edeveloper-pervasive-date-days-since-ad-in-php.html</link>
		<comments>http://www.martienus.com/code/convert-magic-edeveloper-pervasive-date-days-since-ad-in-php.html#comments</comments>
		<pubDate>Fri, 21 Dec 2007 18:07:34 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[Date Format]]></category>
		<category><![CDATA[Magic eDeveloper]]></category>
		<category><![CDATA[Pervasive]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.martienus.com/code/convert-magic-edeveloper-pervasive-date-days-since-ad-in-php.html</guid>
		<description><![CDATA[A while ago I was working on a PHP web-application that uses a Pervasive SQL database which belonged to a Magic eDeveloper application. I encountered a really weird date format in the database which I couldn&#8217;t really place. I had a hunch that it might be the amount of days since AD (01-01-0000). This turned [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I was working on a PHP web-application that uses a Pervasive SQL database which belonged to a Magic eDeveloper application. I encountered a really weird date format in the database which I couldn&#8217;t really place. I had a hunch that it might be the amount of days since AD (01-01-0000). This turned out to be right. Me and a friend devised a way to convert these dates to a unix timestamp in the following manner:</p>
<ul>
<li>Determine the amount of days from AD (01-01-0000) to the Unix epoch (01-01-1970). Outcome: 719163.</li>
<li>Subtract the weird date with the amount determined above.</li>
<li>Multiply the outcome by the amount of seconds in a day (86400).</li>
</ul>
<p>Presto, there you have your unix timestamp.</p>
<p>The code in PHP:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// A simple way to convert Magic date to a unix timestamp and vice versa</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AD_TO_UNIXEPOCH&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">719163</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SECONDS_IN_DAY&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">86400</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Function that converts the days to unixtime</span>
<span style="color: #000000; font-weight: bold;">function</span> days2unixtime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span> <span style="color: #339933;">-</span> AD_TO_UNIXEPOCH<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> SECONDS_IN_DAY<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// The other way around</span>
<span style="color: #000000; font-weight: bold;">function</span> unixtime2days<span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">return</span> AD_TO_UNIXEPOCH <span style="color: #339933;">+</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span> <span style="color: #339933;">/</span> SECONDS_IN_DAY<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Proof of concept</span>
<span style="color: #000088;">$magicdate</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">732468</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$unixtime</span> <span style="color: #339933;">=</span> days2unixtime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$magicdate</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #990000;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%d</span>-%m-%Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$unixtime</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Prepare for a hellish output ;)</span>
<span style="color: #b1b100;">print</span> unixtime2days<span style="color: #009900;">&#40;</span><span style="color: #000088;">$unixtime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// And converted back</span></pre></td></tr></table></div>

<p>NB: For dates before the Unix epoch you will get a negative result.</p>
<p>Hope this helps someone who&#8217;s encountered the same problem.<br />
If you know a better solution please leave a comment or mail.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martienus.com/code/convert-magic-edeveloper-pervasive-date-days-since-ad-in-php.html/feed</wfw:commentRss>
		<slash:comments>8672</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.678 seconds -->

