Skip to content

Commit c0c801d

Browse files
committed
Fixed bug #52015 (Allow including end date in DatePeriod iterations)
1 parent 852e6b9 commit c0c801d

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

ext/date/php_date.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,7 @@ PHP_FUNCTION(getdate)
14301430
#define PHP_DATE_TIMEZONE_PER_COUNTRY 0x1000
14311431

14321432
#define PHP_DATE_PERIOD_EXCLUDE_START_DATE 0x0001
1433+
#define PHP_DATE_PERIOD_INCLUDE_END_DATE 0x0002
14331434

14341435

14351436
/* define an overloaded iterator structure */
@@ -1470,7 +1471,11 @@ static int date_period_it_has_more(zend_object_iterator *iter)
14701471
php_period_obj *object = Z_PHPPERIOD_P(&iterator->intern.data);
14711472

14721473
if (object->end) {
1473-
return object->current->sse < object->end->sse ? SUCCESS : FAILURE;
1474+
if (object->include_end_date) {
1475+
return object->current->sse <= object->end->sse ? SUCCESS : FAILURE;
1476+
} else {
1477+
return object->current->sse < object->end->sse ? SUCCESS : FAILURE;
1478+
}
14741479
} else {
14751480
return (iterator->current_index < object->recurrences) ? SUCCESS : FAILURE;
14761481
}
@@ -1734,6 +1739,7 @@ static void date_register_classes(void) /* {{{ */
17341739
zend_declare_class_constant_long(date_ce_period, const_name, sizeof(const_name)-1, value);
17351740

17361741
REGISTER_PERIOD_CLASS_CONST_STRING("EXCLUDE_START_DATE", PHP_DATE_PERIOD_EXCLUDE_START_DATE);
1742+
REGISTER_PERIOD_CLASS_CONST_STRING("INCLUDE_END_DATE", PHP_DATE_PERIOD_INCLUDE_END_DATE);
17371743
} /* }}} */
17381744

17391745
static zend_object *date_object_new_date(zend_class_entry *class_type) /* {{{ */
@@ -4567,9 +4573,10 @@ PHP_METHOD(DatePeriod, __construct)
45674573

45684574
/* options */
45694575
dpobj->include_start_date = !(options & PHP_DATE_PERIOD_EXCLUDE_START_DATE);
4576+
dpobj->include_end_date = options & PHP_DATE_PERIOD_INCLUDE_END_DATE;
45704577

45714578
/* recurrrences */
4572-
dpobj->recurrences = recurrences + dpobj->include_start_date;
4579+
dpobj->recurrences = recurrences + dpobj->include_start_date + dpobj->include_end_date;
45734580

45744581
dpobj->initialized = 1;
45754582
}

ext/date/php_date.h

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ struct _php_period_obj {
9393
int recurrences;
9494
bool initialized;
9595
bool include_start_date;
96+
int include_end_date;
9697
zend_object std;
9798
};
9899

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
DatePeriod::INCLUDE_END_DATE
3+
--FILE--
4+
<?php
5+
date_default_timezone_set('UTC');
6+
$start = new DateTime('2010-06-07');
7+
$end = new DateTime('2010-06-10');
8+
$interval = new DateInterval('P1D');
9+
10+
foreach (new DatePeriod($start, $interval, $end, DatePeriod::INCLUDE_END_DATE) as $day) {
11+
echo $day->format('Y-m-d') . "\n";
12+
}
13+
?>
14+
--EXPECT--
15+
2010-06-07
16+
2010-06-08
17+
2010-06-09
18+
2010-06-10
19+

0 commit comments

Comments
 (0)