<?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>Val Petruchek on Web-Development &#187; datetime</title>
	<atom:link href="http://petruchek.com/tag/datetime/feed/" rel="self" type="application/rss+xml" />
	<link>http://petruchek.com</link>
	<description>PHP, JavaScript, MySQL, Apache, CSS</description>
	<lastBuildDate>Sun, 13 Sep 2020 18:58:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>It&#8217;s not my birthday, it&#8217;s a NEW YEAR, dumbass.</title>
		<link>http://petruchek.com/2015/01/its-a-new-year-dumbass/</link>
		<comments>http://petruchek.com/2015/01/its-a-new-year-dumbass/#comments</comments>
		<pubDate>Fri, 02 Jan 2015 12:14:32 +0000</pubDate>
		<dc:creator>val</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[null]]></category>
		<category><![CDATA[timestamp]]></category>

		<guid isPermaLink="false">http://petruchek.com/?p=193</guid>
		<description><![CDATA[On the January 1st this year I got one very odd email from online store I purchased something from couple of years ago (BTW, it was the worst online shopping experience ever: it worked like a soviet hospital, not an &#8230; <a href="http://petruchek.com/2015/01/its-a-new-year-dumbass/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><img src="http://petruchek.com/wp-content/uploads/2015/01/happy-birthday-and-happy-new-year.png" alt="happy-birthday-and-happy-new-year" width="300" height="350" class="alignnone size-full wp-image-199" align="left" style="margin:10px !important" />On the January 1st this year I got one very odd email from online store I purchased something from couple of years ago (<em>BTW, it was the worst online shopping experience ever: it worked like a soviet hospital, not an internet shop</em>).</p>
<p>Anyway, it was a &#8216;<strong>Happy Birthday</strong>&#8216; email, and no, my birthday is not the January, 1st.</p>
<p>It&#8217;s very unlikely that I entered 01/01 as my DOB: I try not to tell it to online stores when the field is not required; even when it is I prefer to enter 02/29 to minimize the number of such automated HB congratulations.</p>
<p>So it got me thinking: <strong>why on earth would their database contain 01/01 as my DOB? </strong></p>
<p>My best guess is that they are storing date of birth as a <a href="unixtimestamp.com">unix timestamp</a>. And at some point the value I provided (NULL) got converted to 0 (zero).</p>
<p>And zero seconds since the Epoch means 01/01/1970: so, their software decided I just turned 45 and sent me a happy birthday email (I didn&#8217;t think it was possible, but my opinion about that particular store got worse).</p>
<p><u>What lessons should developers learn from this story?</u></p>
<ol>
<li>Never use TIMESTAMP when you can use DATE data type. They are different, and <strong>date</strong> of birth is definitely a DATE.</li>
<li>Do not confuse NULL and zero (or empty string). NULL is a special value, meaning &#8216;NOTHING&#8217;. There are cases, when it&#8217;s the same as zero (e.g.: number of items purchased), but generally it&#8217;s not the same (as in my example with non-entered DOB).</li>
<li>Use DEFAULT values properly. 0 and NULL are not necessary the same.</li>
<li>Treat data carefully during upgrades/conversions. I&#8217;m not sure if the DOB field was there when I was registering at the website, but it could be introduced in the database later &#8211; and all old records may have got their values for DOB field set to 0 instead of NULL. </li>
</ol>
<p>In my last guess is true, I&#8217;m not the only one who got that stupid &#8216;Happy Birthday&#8217; notification when we should have received &#8216;Happy New Year&#8217; notification instead.</p>
<p><strong>Happy New Year to all (if any) readers of this blog!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://petruchek.com/2015/01/its-a-new-year-dumbass/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get number of days in month with PHP</title>
		<link>http://petruchek.com/2011/08/get-number-of-days-in-month-with-php/</link>
		<comments>http://petruchek.com/2011/08/get-number-of-days-in-month-with-php/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 09:00:51 +0000</pubDate>
		<dc:creator>val</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://petruchek.com/?p=47</guid>
		<description><![CDATA[Problem: get number of days in certain month with PHP, if $m – is a month and $y – is an year. First solution, hacker-style (source): (31&#160;-&#160;(($m&#160;-&#160;1)&#160;%&#160;7&#160;%&#160;2)&#160;-&#160;((($m&#160;==&#160;2)&#160;&#60;&#60;&#160;!!($y&#160;%&#160;4))))&#160;?&#62; This solution doesn’t work properly for years divisible by 100, and non-divisible by &#8230; <a href="http://petruchek.com/2011/08/get-number-of-days-in-month-with-php/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Problem: get number of days in certain month with PHP, if <strong>$m</strong> – is a month and <strong>$y</strong> – is an year.</p>
<p><strong>First solution, <em>hacker-style</em></strong> (<a href="http://ibash.org.ru/quote.php?id=12267" target="_blank">source</a>):</p>
<p><code><span style="color:#000000"><span style="color:#007700">(</span><span style="color:#0000bb">31&nbsp;</span><span style="color:#007700">-&nbsp;((</span><span style="color:#0000bb">$m&nbsp;</span><span style="color:#007700">-&nbsp;</span><span style="color:#0000bb">1</span><span style="color:#007700">)&nbsp;%&nbsp;</span><span style="color:#0000bb">7&nbsp;</span><span style="color:#007700">%&nbsp;</span><span style="color:#0000bb">2</span><span style="color:#007700">)&nbsp;-&nbsp;(((</span><span style="color:#0000bb">$m&nbsp;</span><span style="color:#007700">==&nbsp;</span><span style="color:#0000bb">2</span><span style="color:#007700">)&nbsp;&lt;&lt;&nbsp;!!(</span><span style="color:#0000bb">$y&nbsp;</span><span style="color:#007700">%&nbsp;</span><span style="color:#0000bb">4</span><span style="color:#007700">))))&nbsp;</span><span style="color:#0000bb">?&gt;</span></span></code></p>
<p>This solution doesn’t work properly for years divisible by 100, and non-divisible by 400 (years 1900 and 2100 are not leap)</p>
<p><strong>Second solution, <em>canonical</em>:</strong></p>
<p><code><span style="color:#000000"><span style="color:#0000bb">date</span><span style="color:#007700">(</span><span style="color:#dd0000">"t"</span><span style="color:#007700">,</span><span style="color:#0000bb">mktime</span><span style="color:#007700">(</span><span style="color:#0000bb">0</span><span style="color:#007700">,</span><span style="color:#0000bb">0</span><span style="color:#007700">,</span><span style="color:#0000bb">0</span><span style="color:#007700">,</span><span style="color:#0000bb">$m</span><span style="color:#007700">,</span><span style="color:#0000bb">1</span><span style="color:#007700">,</span><span style="color:#0000bb">$y</span><span style="color:#007700">));</span></span></code></p>
<p>This solution works only for dates which can be presented as unix timestamps, therefore it’s not good (depends on PHP version and OS.)</p>
<p><strong>Third solution, <em>proper</em></strong>:</p>
<p><code><span style="color:#000000"><span style="color:#007700">function&nbsp;</span><span style="color:#0000bb;font-weight:bold">days_in_month</span><span style="color:#007700">(</span><span style="color:#0000bb">$y</span><span style="color:#007700">,</span><span style="color:#0000bb">$m</span><span style="color:#007700">)<br />
{<span style="color:#ff8000">//by&nbsp;val&nbsp;petruchek&nbsp;http://petruchek.com</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#0000bb">$d&nbsp;</span><span style="color:#007700">=&nbsp;</span><span style="color:#0000bb">31</span><span style="color:#007700">;<br />
&nbsp;&nbsp;&nbsp;&nbsp;while(!</span><span style="color:#0000bb">checkdate</span><span style="color:#007700">(</span><span style="color:#0000bb">$m</span><span style="color:#007700">,</span><span style="color:#0000bb">$d</span><span style="color:#007700">,</span><span style="color:#0000bb">$y</span><span style="color:#007700">))&nbsp;</span><span style="color:#0000bb">$d</span><span style="color:#007700">--;<br />
&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color:#0000bb">$d</span><span style="color:#007700">;<br />
}</span></span></code></p>
<p>This solution is based on built-in PHP function checkdate() which validates Gregorian date without unix timestamps.</p>
]]></content:encoded>
			<wfw:commentRss>http://petruchek.com/2011/08/get-number-of-days-in-month-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
