Skip to content

Commit 62129f3

Browse files
committed
Make DatePeriod support DateTimeImmutable as well.
If the start element is a DateTimeImmutable object, then all returned objects are also DateTimeImmutable objects. If the start element is a DateTime object, then all returned objects are DateTime objects.
1 parent 793b52b commit 62129f3

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

ext/date/php_date.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1847,7 +1847,7 @@ static void date_period_it_current_data(zend_object_iterator *iter, zval ***data
18471847

18481848
/* Create new object */
18491849
MAKE_STD_ZVAL(iterator->current);
1850-
php_date_instantiate(date_ce_date, iterator->current TSRMLS_CC);
1850+
php_date_instantiate(object->start_ce, iterator->current TSRMLS_CC);
18511851
newdateobj = (php_date_obj *) zend_object_store_get_object(iterator->current TSRMLS_CC);
18521852
newdateobj->time = timelib_time_ctor();
18531853
*newdateobj->time = *it_time;
@@ -4182,6 +4182,7 @@ PHP_METHOD(DatePeriod, __construct)
41824182
if (dpobj->end) {
41834183
timelib_update_ts(dpobj->end, NULL);
41844184
}
4185+
dpobj->start_ce = date_ce_date;
41854186
} else {
41864187
/* init */
41874188
intobj = (php_interval_obj *) zend_object_store_get_object(interval TSRMLS_CC);
@@ -4197,6 +4198,7 @@ PHP_METHOD(DatePeriod, __construct)
41974198
clone->tz_info = dateobj->time->tz_info;
41984199
}
41994200
dpobj->start = clone;
4201+
dpobj->start_ce = Z_OBJCE_P(start);
42004202

42014203
/* interval */
42024204
dpobj->interval = timelib_rel_time_clone(intobj->diff);

ext/date/php_date.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ struct _php_interval_obj {
154154
struct _php_period_obj {
155155
zend_object std;
156156
timelib_time *start;
157+
zend_class_entry *start_ce;
157158
timelib_time *current;
158159
timelib_time *end;
159160
timelib_rel_time *interval;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
--TEST--
2+
DatePeriod
3+
--FILE--
4+
<?php
5+
date_default_timezone_set('UTC');
6+
$db1 = new DateTimeImmutable( '2008-01-01' );
7+
$db2 = new DateTime( '2008-01-01' );
8+
$de = new DateTime( '2008-03-31' );
9+
$di = DateInterval::createFromDateString( 'first day of next month' );
10+
11+
foreach ( new DatePeriod( $db1, $di, $de ) as $dt )
12+
{
13+
echo get_class( $dt ), "\n";
14+
echo $dt->format( "l Y-m-d\n" );
15+
echo $dt->modify( "3 tuesday" )->format( "l Y-m-d\n" );
16+
echo $dt->format( "l Y-m-d\n\n" );
17+
}
18+
19+
foreach ( new DatePeriod( $db2, $di, $de ) as $dt )
20+
{
21+
echo get_class( $dt ), "\n";
22+
echo $dt->format( "l Y-m-d\n" );
23+
echo $dt->modify( "3 tuesday" )->format( "l Y-m-d\n" );
24+
echo $dt->format( "l Y-m-d\n\n" );
25+
}
26+
?>
27+
--EXPECT--
28+
DateTimeImmutable
29+
Tuesday 2008-01-01
30+
Tuesday 2008-01-15
31+
Tuesday 2008-01-01
32+
33+
DateTimeImmutable
34+
Friday 2008-02-01
35+
Tuesday 2008-02-19
36+
Friday 2008-02-01
37+
38+
DateTimeImmutable
39+
Saturday 2008-03-01
40+
Tuesday 2008-03-18
41+
Saturday 2008-03-01
42+
43+
DateTime
44+
Tuesday 2008-01-01
45+
Tuesday 2008-01-15
46+
Tuesday 2008-01-15
47+
48+
DateTime
49+
Friday 2008-02-01
50+
Tuesday 2008-02-19
51+
Tuesday 2008-02-19
52+
53+
DateTime
54+
Saturday 2008-03-01
55+
Tuesday 2008-03-18
56+
Tuesday 2008-03-18

0 commit comments

Comments
 (0)