{"id":72,"date":"2019-09-07T06:15:11","date_gmt":"2019-09-07T06:15:11","guid":{"rendered":"https:\/\/tutorialio.com\/?p=72"},"modified":"2019-09-07T06:15:11","modified_gmt":"2019-09-07T06:15:11","slug":"php-dateperiod-class-methods","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/php-dateperiod-class-methods\/","title":{"rendered":"PHP DatePeriod Class Methods"},"content":{"rendered":"<table class=\"quick-links\">\n<caption>DatePeriod class Methods in PHP<\/caption>\n<\/caption>\n<tbody>\n<tr>\n<th>Function Name<\/th>\n<th>Function Description<\/th>\n<\/tr>\n<tr>\n<td><a href=\"#dateperiodconstruct\">DatePeriod::__construct()<\/a><\/td>\n<td>Creates a new DatePeriod object<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#dateperiodgetdateinterval\">DatePeriod::getDateInterval()<\/a><\/td>\n<td>Gets the interval<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#dateperiodgetenddate\">DatePeriod::getEndDate()<\/a><\/td>\n<td>Gets the end date<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#dateperiodgetrecurrences\">DatePeriod::getRecurrences()<\/a><\/td>\n<td>Gets the number of recurrences<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#dateperiodgetstartdate\">DatePeriod::getStartDate()<\/a><\/td>\n<td>Gets the start date<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"dateperiodconstruct\">PHP <code>DatePeriod::__construct()<\/code> Method<\/h2>\n<h3>What does <code>DatePeriod::__construct()<\/code> do?<\/h3>\n<p>The PHP <code>DatePeriod::<code>__construct()<\/code><\/code> method creates a new DatePeriod object.<\/p>\n<h3>PHP <code>DatePeriod::__construct()<\/code> Syntax<\/h3>\n<h3>PHP <code>DatePeriod::__construct()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>start<\/code> \u2014 The start date of the period. <\/p>\n<\/li>\n<li>\n<p><code>interval<\/code> \u2014 The interval between recurrences within the period. <\/p>\n<\/li>\n<li>\n<p><code>recurrences<\/code> \u2014 The number of recurrences. <\/p>\n<\/li>\n<li>\n<p><code>end<\/code> \u2014 The end date of the period. <\/p>\n<\/li>\n<li>\n<p><code>isostr<\/code> \u2014 An ISO 8601 repeating interval specification. <\/p>\n<\/li>\n<li>\n<p><code>options<\/code> \u2014 Can be set to DatePeriod::EXCLUDE_START_DATE to exclude the start date from the set of recurring dates within the period. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>DatePeriod::__construct()<\/code> Working Examples<\/h3>\n<h4>1. DatePeriod example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$start = new DateTime('2012-07-01');\n$interval = new DateInterval('P7D');\n$end = new DateTime('2012-07-31');\n$recurrences = 4;\n$iso = 'R4\/2012-07-01T00:00:00Z\/P7D';\n\/\/ All of these periods are equivalent.\n$period = new DatePeriod($start, $interval, $recurrences);\n$period = new DatePeriod($start, $interval, $end);\n$period = new DatePeriod($iso);\n\/\/ By iterating over the DatePeriod object, all of the\n\/\/ recurring dates within that period are printed.\nforeach ($period as $date) {\n    echo $date-&gt;format('Y-m-d').\"\\n\";\n}\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2012-07-01\n2012-07-08\n2012-07-15\n2012-07-22\n2012-07-29<\/code><\/pre>\n<h4>2. DatePeriod example with DatePeriod::EXCLUDE_START_DATE<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$start = new DateTime('2012-07-01');\n$interval = new DateInterval('P7D');\n$end = new DateTime('2012-07-31');\n$period = new DatePeriod($start, $interval, $end,\n                         DatePeriod::EXCLUDE_START_DATE);\n\/\/ By iterating over the DatePeriod object, all of the\n\/\/ recurring dates within that period are printed.\n\/\/ Note that, in this case, 2012-07-01 is not printed.\nforeach ($period as $date) {\n    echo $date-&gt;format('Y-m-d').\"\\n\";\n}\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2012-07-08\n2012-07-15\n2012-07-22\n2012-07-29<\/code><\/pre>\n<h3>Changelog for PHP DatePeriod::__construct()<\/code> Method<\/h3>\n<p><strong>5.5.8 \u2014 <\/strong> <code class=\"parameter\">end<\/code> type changed to DateTimeInterface. Previously, DateTime. <\/p>\n<p><strong>5.5.0 \u2014 <\/strong> <code class=\"parameter\">start<\/code> type changed to DateTimeInterface. Previously, DateTime. <\/p>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: simon dot kohlmeyer<\/p>\n<pre><code class=\"lang-xxxx\">I found two things useful to know that aren't covered here. \n \n1. endDate is excluded: \n \n&lt;?php \n$i = new DateInterval('P1D'); \n$d1 = new Datetime(); \n$d2 = clone $d1; $d2-&gt;add($i); \nforeach(new DatePeriod($d1, $i, $d2) as $d) { \n    echo $d-&gt;format('Y-m-d H:i:s') . \"\\n\"; \n} \n?&gt; \n \nWill output: \n2010-11-03 12:39:53 \n \n(Another one because I got it wrong at first) \n2. For the first form, recurrences really means REcurrences, not occurences. \n \n&lt;?php \n$i = new DateInterval('P1D'); \n$d = new Datetime(); \nforeach(new DatePeriod($d, $i, 1) as $d) { \n    echo $d-&gt;format('Y-m-d H:i:s') . \"\\n\"; \n} \n?&gt; \n \nWill output: \n2010-11-03 12:41:05 \n2010-11-04 12:41:05<\/code><\/pre>\n<p>Contributed By: lars<\/p>\n<pre><code class=\"lang-xxxx\">When you add the time 23:59:59 to the end DateTime object something like the following then the end date will be included in the period: \n \n&lt;?php \n$date_start = new DateTime('2012-03-12'); \n$date_end = new DateTime('2012-03-22 23:59:59'); \n \n$interval = '+2 days'; \n$date_interval = DateInterval::createFromDateString($interval); \n \n$period = new DatePeriod($date_start, $date_interval, $date_end, DatePeriod::EXCLUDE_START_DATE); \n \nforeach($period as $dt) { \n echo $dt-&gt;format('d\/m'); \n} \n?&gt; \n \nOUTPUT: \n14\/03 \n16\/03 \n18\/03 \n20\/03 \n22\/03<\/code><\/pre>\n<h2 id=\"dateperiodgetdateinterval\">PHP <code>DatePeriod::getDateInterval()<\/code> Method<\/h2>\n<h3>What does <code>DatePeriod::getDateInterval()<\/code> do?<\/h3>\n<p>The PHP <code>DatePeriod::<code>getDateInterval()<\/code><\/code> method gets the interval .<\/p>\n<h3>PHP <code>DatePeriod::getDateInterval()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> public DatePeriod::getDateInterval ( void ) : DateInterval<\/code><\/pre>\n<h3>PHP <code>DatePeriod::getDateInterval()<\/code> Parameters<\/h3>\n<ol><\/ol>\n<p>This method does not accept any parameters.<\/p>\n<h3>PHP <code>DatePeriod::getDateInterval()<\/code> Return Value<\/h3>\n<p>The PHP <code>DatePeriod::getDateInterval()<\/code> method returns a DateInterval object<\/p>\n<h3>PHP <code>DatePeriod::getDateInterval()<\/code> Working Examples<\/h3>\n<h4>1. DatePeriod::getDateInterval() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$period = new DatePeriod('R7\/2016-05-16T00:00:00Z\/P1D');\n$interval = $period-&gt;getDateInterval();\necho $interval-&gt;format('%d day');\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">1 day<\/code><\/pre>\n<h2 id=\"dateperiodgetenddate\">PHP <code>DatePeriod::getEndDate()<\/code> Method<\/h2>\n<h3>What does <code>DatePeriod::getEndDate()<\/code> do?<\/h3>\n<p>The PHP <code>DatePeriod::<code>getEndDate()<\/code><\/code> method gets the end date .<\/p>\n<h3>PHP <code>DatePeriod::getEndDate()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> public DatePeriod::getEndDate ( void ) : DateTimeInterface<\/code><\/pre>\n<h3>PHP <code>DatePeriod::getEndDate()<\/code> Parameters<\/h3>\n<ol><\/ol>\n<p>This method does not accept any parameters.<\/p>\n<h3>PHP <code>DatePeriod::getEndDate()<\/code> Return Value<\/h3>\n<p>The PHP <code>DatePeriod::getEndDate()<\/code> method returns <code>NULL<\/code> if the DatePeriod does not have an end date. For example, when initialized with the <code class=\"parameter\">recurrences<\/code> parameter, or the <code class=\"parameter\">isostr<\/code> parameter without an end date.<\/p>\n<h3>PHP <code>DatePeriod::getEndDate()<\/code> Working Examples<\/h3>\n<h4>1. DatePeriod::getEndDate() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$period = new DatePeriod(\n    new DateTime('2016-05-16T00:00:00Z'),\n    new DateInterval('P1D'),\n    new DateTime('2016-05-20T00:00:00Z')\n);\n$start = $period-&gt;getEndDate();\necho $start-&gt;format(DateTime::ISO8601);\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2016-05-20T00:00:00+0000<\/code><\/pre>\n<h4>2. DatePeriod::getEndDate() without an end date<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$period = new DatePeriod(\n    new DateTime('2016-05-16T00:00:00Z'),\n    new DateInterval('P1D'),\n    7\n);\nvar_dump($period-&gt;getEndDate());\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">NULL<\/code><\/pre>\n<h2 id=\"dateperiodgetrecurrences\">PHP <code>DatePeriod::getRecurrences()<\/code> Method<\/h2>\n<h3>What does <code>DatePeriod::getRecurrences()<\/code> do?<\/h3>\n<p>The PHP <code>DatePeriod::<code>getRecurrences()<\/code><\/code> method gets the number of recurrences.<\/p>\n<h3>PHP <code>DatePeriod::getRecurrences()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> public DatePeriod::getRecurrences ( void ) : int<\/code><\/pre>\n<h3>PHP <code>DatePeriod::getRecurrences()<\/code> Parameters<\/h3>\n<ol><\/ol>\n<p>This method does not accept any parameters.<\/p>\n<h3>PHP <code>DatePeriod::getRecurrences()<\/code> Return Value<\/h3>\n<p>The PHP <code>DatePeriod::getRecurrences()<\/code> method returns the number of recurrences.<\/p>\n<h2 id=\"dateperiodgetstartdate\">PHP <code>DatePeriod::getStartDate()<\/code> Method<\/h2>\n<h3>What does <code>DatePeriod::getStartDate()<\/code> do?<\/h3>\n<p>The PHP <code>DatePeriod::<code>getStartDate()<\/code><\/code> method gets the start date .<\/p>\n<h3>PHP <code>DatePeriod::getStartDate()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> public DatePeriod::getStartDate ( void ) : DateTimeInterface<\/code><\/pre>\n<h3>PHP <code>DatePeriod::getStartDate()<\/code> Parameters<\/h3>\n<ol><\/ol>\n<p>This method does not accept any parameters.<\/p>\n<h3>PHP <code>DatePeriod::getStartDate()<\/code> Return Value<\/h3>\n<p>The PHP <code>DatePeriod::getStartDate()<\/code> method returns a DateTimeImmutable object when the DatePeriod is initialized with a DateTimeImmutable object as the <code class=\"parameter\">start<\/code> parameter.<\/p>\n<h3>PHP <code>DatePeriod::getStartDate()<\/code> Working Examples<\/h3>\n<h4>1. DatePeriod::getStartDate() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$period = new DatePeriod('R7\/2016-05-16T00:00:00Z\/P1D');\n$start = $period-&gt;getStartDate();\necho $start-&gt;format(DateTime::ISO8601);\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2016-05-16T00:00:00+0000<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>DatePeriod class Methods in PHP Function Name Function Description DatePeriod::__construct() Creates a new DatePeriod object DatePeriod::getDateInterval() Gets the interval DatePeriod::getEndDate() Gets the end date DatePeriod::getRecurrences() Gets the number of recurrences DatePeriod::getStartDate() Gets the start date PHP DatePeriod::__construct() Method What does DatePeriod::__construct() do? The PHP DatePeriod::__construct() method creates a new DatePeriod object. PHP DatePeriod::__construct() Syntax PHP [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[9,5],"class_list":["post-72","post","type-post","status-publish","format-standard","hentry","category-reference","tag-date-time","tag-php"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/72","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=72"}],"version-history":[{"count":0,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}