Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
23 lines (19 loc) · 789 Bytes

File metadata and controls

23 lines (19 loc) · 789 Bytes
Copy raw file
Download raw file
Edit and raw actions

DateTimeImmutable vs DateTime

  • DateTime示例

      $date = new DateTime();
      $tomorrow = $date->modify('+1 day');
      echo $date->format('Y-m-d');
      echo $tomorrow->format('Y-m-d');	
    
    • This will output:

        2021-05-15
        2021-05-15
      
    • What happened here is that the modify returned the same instance of the DateTime object. The variable $tomorrow doesn't contain a different object, it contains a reference to the original one.

  • DateTimeImmutable示例

      $date = new DateTimeImmutable();
      $tomorrow = $date->modify('+1 day');
      echo $date->format('Y-m-d');
      echo $tomorrow->format('Y-m-d');	
    
    • This will output:

        2021-05-14
        2021-05-15
      
    • Because in DateTimeImmutable, the modification methods don't return the same instance, they give you a fresh one (also immutable).

Morty Proxy This is a proxified and sanitized view of the page, visit original site.