{"id":2276,"date":"2014-09-23T12:58:59","date_gmt":"2014-09-23T10:58:59","guid":{"rendered":"http:\/\/www.nyamsprod.com\/blog\/?p=2276"},"modified":"2014-09-23T14:07:05","modified_gmt":"2014-09-23T12:07:05","slug":"specifying-the-endpoints-of-a-time-range","status":"publish","type":"post","link":"https:\/\/nyamsprod.com\/blog\/specifying-the-endpoints-of-a-time-range\/","title":{"rendered":"Specifying the endpoints of a time range"},"content":{"rendered":"<div class=\"message warning\">\n<p><strong>Attention:<\/strong> Les informations de ce billet sont susceptibles d'&ecirc;tre obsol&egrave;tes car vieux de plus 2 ans.<\/p>\n<p><strong>Warning: <\/strong> The information you are reading may be obsolete, this post was published more than 2 years ago.<\/p>\n<\/div><p>In PHP we hate re-inventing the wheel so when I stumble on <a title=\"Resolving Feature Envy in the Domain\" href=\"http:\/\/verraes.net\/2014\/08\/resolving-feature-envy-in-the-domain\/\" target=\"_blank\">Mathias Verraes post about a ReportingPeriod<\/a> class it just reminded me how often I came across the same problem over and over again in my daily life as a developer. So to resolve this issue, hopefully, once for all, I decided to implement an Immutable Value Object class which will deal with time range. So here I present you <a title=\"Bakame\\Period a Simple PHP Class to manipulate Time Range\" href=\"https:\/\/github.com\/nyamsprod\/Period\" target=\"_blank\"><code>Bakame\\Period<\/code><\/a><!--more--><\/p>\n<p>If you look at the <a title=\"Bakame\\Period source code\" href=\"https:\/\/github.com\/nyamsprod\/Period\/blob\/master\/src\/Period.php\" target=\"_blank\">Github source code<\/a>, you&#8217;ll see that the class is very simple and only requires PHP 5.3+ . The only problem that I run into when creating this class was specifying its endpoints values.<\/p>\n<p>To understand the solution that I implemented I&#8217;ll take two examples:<\/p>\n<p>First let&#8217;s assume you are booking your holidays so you need to tell your boss when you will be back at work. You have at least two options to say it:<\/p>\n<blockquote><p>I&#8217;ll take my holiday from January 1st to January 7th included.<\/p><\/blockquote>\n<p>Or<\/p>\n<blockquote><p>I&#8217;ll take my holiday on January 1st and I&#8217;ll be back at work on the 8th of January.<\/p><\/blockquote>\n<p>Both sentences convey the same information but note that in the first one you had to add the <em><strong>included<\/strong> <\/em>word in your statement to remove any ambiguity. Otherwise, your boss would have think that you would be back at work on the 7th of January. Telling your boss the real end of your holiday is not always a smart move \ud83d\ude42 .<\/p>\n<p>Now let&#8217;s look at another situation that will talk more to us developers (..because developers never take holidays). Look at the following queries.<\/p>\n<pre><code class=\"language-php\">$query1 = \"SELECT COUNT(*) FROM users WHERE create_at&gt;= '201-06-05 00:00:00' AND create_at &lt;= '2014-06-05 23:59:59'\";\r\n$query2 = \"SELECT COUNT(*) FROM users WHERE create_at&gt;= '201-06-05 00:00:00' AND create_at &lt; '2014-06-06 00:00:00'\";<\/code><\/pre>\n<ul>\n<li>The first query uses endpoints included in the interval;<\/li>\n<li>The second query uses the included endpoint for the range beginning but the excluded endpoint for the range end.<\/li>\n<\/ul>\n<p>Both queries seem to return the same value but in reality they don&#8217;t. if your SQL engine supports micro-seconds which I think it already does <a title=\"MySQL supports fractional seconds since version 5.6.4\" href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.6\/en\/fractional-seconds.html\" target=\"_blank\">in recent versions of MySQL<\/a>, for instance, you will be missing some microseconds in your query. So if these queries return the same value in your SQL engine, it&#8217;s only because of an implementation limitation not because they should in the first place !! (As a side note this is why I hate using the BETWEEN statement in MySQL queries when dealing with date fields).<\/p>\n<p>In summary, to be on the safe side of time range manipulation it is better to specify:<\/p>\n<ul>\n<li>the included starting time as one endpoint;<\/li>\n<li>the excluded ending time as the other endpoint;<\/li>\n<\/ul>\n<p>Even if it may sound odd in the beginning after some usages you&#8217;ll feel that the tradeoff is far less important than the clarity and usage gain. And that&#8217;s why when using <code>\\Bakame\\Period<\/code> objects you&#8217;ll notice the following behaviour.<\/p>\n<pre><code class=\"language-php\">&lt;?php\r\nuse Bakame\\Period; \r\n$period = Period::createFromMonth(2014, 10);\r\n$period-&gt;getStart(); \/\/returns new DateTime('2014-10-01');\r\n$period-&gt;getEnd(); \/\/returns new DateTime('2014-11-01');\r\n$period-&gt;contains($period-&gt;getStart()); \/\/return true;\r\n$period-&gt;contains($period-&gt;getEnd()); \/\/return false;\r\n<\/code><\/pre>\n<p>Happy time range manipulation with <code>Bakame\\Period<\/code>!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tradeoffs and gains when specifying time range endpoints<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[776,771,231,699,777,775],"class_list":["post-2276","post","type-post","status-publish","format-standard","hentry","category-web","tag-bakame","tag-datetime","tag-github","tag-mysql","tag-period","tag-range"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/comments?post=2276"}],"version-history":[{"count":5,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2276\/revisions"}],"predecessor-version":[{"id":2310,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2276\/revisions\/2310"}],"wp:attachment":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/media?parent=2276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/categories?post=2276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/tags?post=2276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}