<?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; convert</title>
	<atom:link href="http://www.martienus.com/tag/convert/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>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 0.446 seconds -->

