note 126550 added to dateperiod.construct

From: Date: Mon, 01 Nov 2021 06:58:45 +0000
Subject: note 126550 added to dateperiod.construct
Groups: php.notes 
Request: Send a blank email to php-notes+get-217133@lists.php.net to get a copy of this message
With reference to the Changelog above, 'recurrences' must be greater than 0 now. This was not the case in PHP5 ( >= 5.3.0). A foreach loop over a DatePeriod with recurrences == 0 would execute one time. A function could accept a $count parameter which represented the number of times to execute the code in a foreach loop, and then initialize a DatePeriod with 'recurrences' == ($count-1) to get the desired number of iterations. The following example adds a special case for PHP7 and above to handle the situation where a single iteration is desired. Note that an alternative solution to subracting 1 from the desired count is to subtract one month from the desired starting month, and then intialize the DatePeriod with the option DatePeriod::EXCLUDE_START_DATE. <?php // displayCalender() is a stub. // In a real application, this would generate a displayable calendar for the specified month. // // $tp is a DateTime object used to specify the month for which a calendar will be displayed function displayCalendar($tp) { $format = "F, Y"; echo "Display Calendar for " . $tp->format($format) . "\n"; } // $date is the first month to show // $count is the number of months to show // NOTE: the number of "recurrences" is one less than the number of months to display function showMonths($date = false, $count = 1) { $format = "Y-m-d 00:00:00"; $init = ($date ? $date : date($format)); $one_month = new DateInterval("P1M"); // get the starting month and day $start = new DateTime($init); echo "Show $count month" . ($count > 1 ? "s" : "") . "\n"; //check for PHP7 or greater if ($count == 1 && (substr(phpversion(), 0, 1) >= 7) ) { // Special case for PHP7 when $count == 1 displayCalendar($start); } else { // PHP5 does not need special case, because 0 is accepted value // for number of recurrences $time_period = new DatePeriod($start, $one_month, ($count-1)); foreach ($time_period as $tp) { displayCalendar($tp); } } echo "\n"; } // Show calendar for specific month $first_month = "2021-11"; showMonths($first_month); // Show calendars for a full year $first_month = "2021-01"; showMonths($first_month, 12); ?> The above example will output: Show 1 month Display Calendar for November, 2021 Show 12 months Display Calendar for January, 2021 Display Calendar for February, 2021 Display Calendar for March, 2021 Display Calendar for April, 2021 Display Calendar for May, 2021 Display Calendar for June, 2021 Display Calendar for July, 2021 Display Calendar for August, 2021 Display Calendar for September, 2021 Display Calendar for October, 2021 Display Calendar for November, 2021 Display Calendar for December, 2021 ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 2601:198:300:5140:dacb:8aff:feaa:f209) ---- Manual Page -- https://php.net/manual/en/dateperiod.construct.php Edit -- https://main.php.net/note/edit/126550 Del: integrated -- https://main.php.net/note/delete/126550/integrated Del: useless -- https://main.php.net/note/delete/126550/useless Del: bad code -- https://main.php.net/note/delete/126550/bad+code Del: spam -- https://main.php.net/note/delete/126550/spam Del: non-english -- https://main.php.net/note/delete/126550/non-english Del: in docs -- https://main.php.net/note/delete/126550/in+docs Del: other reasons-- https://main.php.net/note/delete/126550 Reject -- https://main.php.net/note/reject/126550 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217133) next »