Skip to content

[HttpFoundation] Add support for DateTimeImmutable in setters #19727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 9 additions & 8 deletions src/Symfony/Component/HttpFoundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,12 +626,13 @@ public function getDate()
/**
* Sets the Date header.
*
* @param \DateTime $date A \DateTime instance
* @param \DateTimeInterface $date A \DateTimeInterface instance
*
* @return Response
*/
public function setDate(\DateTime $date)
public function setDate(\DateTimeInterface $date)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about renaming it to setDateHeader for bc ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically, it would work yes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another solution could be to deprecate extending this functions and make them final in 4.0 (i prefer this solution).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ener-Getick let's give it a try, would you mind opening a PR doing this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, see #19734

{
$date = \DateTime::createFromFormat(\DateTime::ISO8601, $date->format(\DateTime::ISO8601));
$date->setTimezone(new \DateTimeZone('UTC'));
$this->headers->set('Date', $date->format('D, d M Y H:i:s').' GMT');

Expand Down Expand Up @@ -686,16 +687,16 @@ public function getExpires()
*
* Passing null as value will remove the header.
*
* @param \DateTime|null $date A \DateTime instance or null to remove the header
* @param \DateTimeInterface|null $date A \DateTimeInterface instance or null to remove the header
*
* @return Response
*/
public function setExpires(\DateTime $date = null)
public function setExpires(\DateTimeInterface $date = null)
{
if (null === $date) {
$this->headers->remove('Expires');
} else {
$date = clone $date;
$date = \DateTime::createFromFormat(\DateTime::ISO8601, $date->format(\DateTime::ISO8601));
$date->setTimezone(new \DateTimeZone('UTC'));
$this->headers->set('Expires', $date->format('D, d M Y H:i:s').' GMT');
}
Expand Down Expand Up @@ -826,16 +827,16 @@ public function getLastModified()
*
* Passing null as value will remove the header.
*
* @param \DateTime|null $date A \DateTime instance or null to remove the header
* @param \DateTimeInterface|null $date A \DateTimeInterface instance or null to remove the header
*
* @return Response
*/
public function setLastModified(\DateTime $date = null)
public function setLastModified(\DateTimeInterface $date = null)
{
if (null === $date) {
$this->headers->remove('Last-Modified');
} else {
$date = clone $date;
$date = \DateTime::createFromFormat(\DateTime::ISO8601, $date->format(\DateTime::ISO8601));
$date->setTimezone(new \DateTimeZone('UTC'));
$this->headers->set('Last-Modified', $date->format('D, d M Y H:i:s').' GMT');
}
Expand Down