Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -3729,13 +3729,29 @@ static void php_date_date_set(zval *object, zend_long y, zend_long m, zend_long
PHP_FUNCTION(date_date_set)
{
zval *object;
zend_long y, m, d;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Olll", &object, date_ce_date, &y, &m, &d) == FAILURE) {
php_date_obj* dateobj;
zend_long y, m, d;
bool y_is_null = 1, m_is_null = 1, d_is_null = 1;

if (zend_parse_method_parameters(
ZEND_NUM_ARGS(), getThis(), "O|l!l!l!",
&object, date_ce_date,
&y, &y_is_null,
&m, &m_is_null,
&d, &d_is_null
) == FAILURE) {
RETURN_THROWS();
}

php_date_date_set(object, y, m, d, return_value);
dateobj = Z_PHPDATE_P(object);

php_date_date_set(
object,
y_is_null ? dateobj->time->y : y,
m_is_null ? dateobj->time->m : m,
d_is_null ? dateobj->time->d : d,
return_value
);

RETURN_OBJ_COPY(Z_OBJ_P(object));
}
Expand All @@ -3744,16 +3760,23 @@ PHP_FUNCTION(date_date_set)
/* {{{ */
PHP_METHOD(DateTimeImmutable, setDate)
{
zval *object, new_object;
zend_long y, m, d;
zval *object = ZEND_THIS, new_object;
php_date_obj* dateobj = Z_PHPDATE_P(object);
zend_long y, m, d;
bool y_is_null = 1, m_is_null = 1, d_is_null = 1;

object = ZEND_THIS;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll", &y, &m, &d) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!l!l!", &y, &y_is_null, &m, &m_is_null, &d, &d_is_null) == FAILURE) {
RETURN_THROWS();
}

date_clone_immutable(object, &new_object);
php_date_date_set(&new_object, y, m, d, return_value);
php_date_date_set(
&new_object,
y_is_null ? dateobj->time->y : y,
m_is_null ? dateobj->time->m : m,
d_is_null ? dateobj->time->d : d,
return_value
);

RETURN_OBJ(Z_OBJ(new_object));
}
Expand Down
6 changes: 3 additions & 3 deletions ext/date/php_date.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function date_diff(
function date_time_set(
DateTime $object, int $hour, int $minute, int $second = 0, int $microsecond = 0): DateTime {}

function date_date_set(DateTime $object, int $year, int $month, int $day): DateTime {}
function date_date_set(DateTime $object, ?int $year = null, ?int $month = null, ?int $day = null): DateTime {}

function date_isodate_set(DateTime $object, int $year, int $week, int $dayOfWeek = 1): DateTime {}

Expand Down Expand Up @@ -426,7 +426,7 @@ public function setTime(int $hour, int $minute, int $second = 0, int $microsecon
* @tentative-return-type
* @alias date_date_set
*/
public function setDate(int $year, int $month, int $day): DateTime {}
public function setDate(?int $year = null, ?int $month = null, ?int $day = null): DateTime {}

/**
* @tentative-return-type
Expand Down Expand Up @@ -536,7 +536,7 @@ public function setTimezone(DateTimeZone $timezone): DateTimeImmutable {}
public function setTime(int $hour, int $minute, int $second = 0, int $microsecond = 0): DateTimeImmutable {}

/** @tentative-return-type */
public function setDate(int $year, int $month, int $day): DateTimeImmutable {}
public function setDate(?int $year = null, ?int $month = null, ?int $day = null): DateTimeImmutable {}

/** @tentative-return-type */
public function setISODate(int $year, int $week, int $dayOfWeek = 1): DateTimeImmutable {}
Expand Down
26 changes: 13 additions & 13 deletions ext/date/php_date_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions ext/date/tests/DateTimeImmutable_setDate_partial.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Test DateTimeImmutable::setDate() function : partial application
--FILE--
<?php
date_default_timezone_set('UTC');
$datetime = new DateTimeImmutable('1234-05-06 00:00:00');

echo $datetime->setDate()->format('c'), "\n";
echo $datetime->setDate(1)->format('c'), "\n";
echo $datetime->setDate(month: 2)->format('c'), "\n";
echo $datetime->setDate(3, day: 4)->format('c'), "\n";
?>
--EXPECT--
1234-05-06T00:00:00+00:00
0001-05-06T00:00:00+00:00
1234-02-06T00:00:00+00:00
0003-05-04T00:00:00+00:00
17 changes: 17 additions & 0 deletions ext/date/tests/DateTime_setDate_partial.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Test DateTime::setDate() function : partial application
--FILE--
<?php
date_default_timezone_set('UTC');
$datetime = new DateTime('1234-05-06 00:00:00');

echo $datetime->setDate()->format('c'), "\n";
echo $datetime->setDate(1)->format('c'), "\n";
echo $datetime->setDate(month: 2)->format('c'), "\n";
echo $datetime->setDate(3, day: 4)->format('c'), "\n";
?>
--EXPECT--
1234-05-06T00:00:00+00:00
0001-05-06T00:00:00+00:00
0001-02-06T00:00:00+00:00
0003-02-04T00:00:00+00:00