{"id":2549,"date":"2016-05-12T14:44:04","date_gmt":"2016-05-12T12:44:04","guid":{"rendered":"http:\/\/nyamsprod.com\/blog\/?p=2549"},"modified":"2016-05-12T14:44:04","modified_gmt":"2016-05-12T12:44:04","slug":"dateperiod-demystified","status":"publish","type":"post","link":"https:\/\/nyamsprod.com\/blog\/dateperiod-demystified\/","title":{"rendered":"DatePeriod demystified"},"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>Have you ever wanted to work with PHP&#8217;s <code>DatePeriod<\/code> object. This object introduced in PHP 5.3.0 enables iterating over a set of dates and times (ie datepoints), recurring at regular intervals, over a given time range. In practice, on instantiation this object produces a <code>Traversable<\/code> that generate <code>DateTimeInterface<\/code> objects. This can be helpful, for instance, if you want to produce data reports at specific datepoints over a time range.<\/p>\n<p>The easiest way to instantiate such object is to give the constructor a starting and an ending datepoints as well as the interval between each recurrence.<\/p>\n<script src=\"https:\/\/gist.github.com\/85fb8c47a1dbe06602a6.js?file=dateperiod-datetime.php\"><\/script>\n<p>By default the ending datepoint is not returned and you can further exclude the starting datepoint by using the class constant <code>DatePeriod::EXCLUDE_START_DATE<\/code>.<\/p>\n<p>With the introduction of the <code>DateTimeImmutable<\/code> object in PHP5.5, and a subsequent bug fix to <code>DatePeriod<\/code> in PHP5.5.8, the object results became rather interesting. To sum it up, when iterating over a <code>DatePeriod<\/code>, the datepoint returned is of the same instance as the starting datepoint. Let&#8217;s illustrate this by taking the first example and using a\u00a0<code>DateTimeImmutable<\/code> object instead as the starting datepoint.<\/p>\n<script src=\"https:\/\/gist.github.com\/85fb8c47a1dbe06602a6.js?file=dateperiod-datetimeimmutable.php\"><\/script>\n<p>Iterating the <code>DatePeriod<\/code> will now return <code>DateTimeImmutable<\/code> objects instead.<\/p>\n<p>Now that we understand how the iteration works, we need to know how to get informations about an already instantiated <code>DatePeriod<\/code> object ?<\/p>\n<p><code>DatePeriod<\/code> exposes its properties using two distinct API that share a common trait, <a href=\"http:\/\/php.net\/DatePeriod\" target=\"_blank\">they are <strong>undocumented<\/strong><\/a>.<\/p>\n<p>If you are on PHP5.6.5+ you <strong>should<\/strong> use the recommend API<\/p>\n<script src=\"https:\/\/gist.github.com\/85fb8c47a1dbe06602a6.js?file=dateperiod-property-api.php\"><\/script>\n<p>If you are using an older version of PHP, you <em>may<\/em> use the fact that <code>DatePeriod<\/code> exposes a copy of its internal state.<\/p>\n<script src=\"https:\/\/gist.github.com\/85fb8c47a1dbe06602a6.js?file=dateperiod-buggy-api.php\"><\/script>\n<p>The latter API is buggy for different reasons:<\/p>\n<ul>\n<li>The datepoint value does not returns the instance expected. In the example we gave it a <code>DateTimeImmutable<\/code> object yet start returns a <code>DateTime<\/code> object<\/li>\n<li>The returned <code>DateInterval<\/code> object is in an invalid state and no usable.<\/li>\n<li>No HHVM version supports this API.<\/li>\n<\/ul>\n<p>What to take from this ?<\/p>\n<ol>\n<li>Always rely on the interface meaning if an object is said to return a <code>DateTimeInterface<\/code> do not try to use methods or property outside of the intended interface as the result may be unexpected.<\/li>\n<li>Upgrade you PHP&#8217;s installation to the latest stable version when possible<\/li>\n<\/ol>\n<p><strong>Post Scriptum:<\/strong> If you want to further manipulate your time range in PHP, I would recommend the use of <a href=\"http:\/\/period.thephpleague.com\/api\/\" target=\"_blank\">Period<\/a>. This library, I maintain, is part of <a href=\"http:\/\/thephpleague.com\" target=\"_blank\">the PHP league<\/a>. Period is composed of a single class that handles time range in a uniform and predicable way. The class which comes with many useful named constructors, compares and modifies time range easily by being an immutable value object just like the <code>DateTimeImmutable<\/code> object.<\/p>\n<p><a href=\"https:\/\/github.com\/thephpleague\/period\" target=\"_blank\">League\\Period is an open sourced project on Github<\/a>, and is easily available using composer. You are, of course, welcome to contribute to the source\u00a0code to improve it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Everything you need to know before using PHP #DatePeriod and more<\/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":[780,781,771,793,412],"class_list":["post-2549","post","type-post","status-publish","format-standard","hentry","category-web","tag-dateinterval","tag-dateperiod","tag-datetime","tag-datetimeimmutable","tag-php"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2549","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=2549"}],"version-history":[{"count":5,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2549\/revisions"}],"predecessor-version":[{"id":2610,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/posts\/2549\/revisions\/2610"}],"wp:attachment":[{"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/media?parent=2549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/categories?post=2549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nyamsprod.com\/blog\/wp-json\/wp\/v2\/tags?post=2549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}