Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7030805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:43:12+00:00 2026-05-28T00:43:12+00:00

I am seeing very strange behaviour, with DateTime choosing to add a day sometimes,

  • 0

I am seeing very strange behaviour, with DateTime choosing to add a day sometimes, but not others.

<?php
// If you're running this after Jan 2012, use: new DateTime(date('Y-m-d', strtotime('2012-01-09')));
$month_end_date = new DateTime();
$month_end_date->modify('last day of this month');

$event_end_date = new DateTime('2012-03-15');

if ($event_end_date > $month_end_date) {
  // Using this line a day is never added on below and the date stays as 31 Jan 2012
  $event_end_date = clone $month_end_date;
  // This line allows the ->add() call to work, and gives 1 Feb 2012 as output:
  #$event_end_date = new DateTime($month_end_date->format('Y-m-d'));
}

$event_end_date->add(new DateInterval('P1D'));

// Date should now be 1st Feb
echo "Should be 1 Feb:   ". $event_end_date->format('Y-m-d');
?>

It appears to be the ->modify('last day of this month') line which breaks my code; it will print 1 Feb 2012 if I replace the first two lines with $month_end_date = new DateTime('2011-01-31'); or

$month_end_date = new DateTime('last day of this month'); 
$month_end_date = new DateTime($month_end_date->format(DateTime::W3C)); 

or use my alternative $event_end_date = new DateTime($month_end_date->format('Y-m-d'));.

Does it make sense that I need to call format before making a second modification?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:43:13+00:00Added an answer on May 28, 2026 at 12:43 am

    It appears the usage of “first” or “last” directly in the constructor of the DateTime object causes it to become immutable. This does seem like a bug.

    e.g.

    date_default_timezone_set('Europe/London');
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    
    $interval = new DateInterval('P1D');
    
    $date1 = new DateTime('last day of this month');
    var_dump($date1);
    $date1->add($interval);
    var_dump($date1);
    
    echo "-------------------------------\n\n";
    
    $lastDayOfMonth = date('Y-m-d H:i:s', strtotime('last day of this month'));
    $date2 = new DateTime($lastDayOfMonth);
    var_dump($date2);
    $date2->add($interval);
    var_dump($date2);
    
    echo "-------------------------------\n\n";
    
    $date3 = new DateTime('2012-01-31 '.date('H:i:s'));
    var_dump($date3);
    $date3->add($interval);
    var_dump($date3);
    

    Results in:

    object(DateTime)[2]
      public 'date' => string '2012-01-31 11:40:10' (length=19)
      public 'timezone_type' => int 3
      public 'timezone' => string 'Europe/London' (length=13)
    
    object(DateTime)[2]
      public 'date' => string '2012-01-31 11:40:10' (length=19)
      public 'timezone_type' => int 3
      public 'timezone' => string 'Europe/London' (length=13)
    
    -------------------------------
    
    object(DateTime)[3]
      public 'date' => string '2012-01-31 11:40:10' (length=19)
      public 'timezone_type' => int 3
      public 'timezone' => string 'Europe/London' (length=13)
    
    object(DateTime)[3]
      public 'date' => string '2012-02-01 11:40:10' (length=19)
      public 'timezone_type' => int 3
      public 'timezone' => string 'Europe/London' (length=13)
    
    -------------------------------
    
    object(DateTime)[4]
      public 'date' => string '2012-01-31 11:40:10' (length=19)
      public 'timezone_type' => int 3
      public 'timezone' => string 'Europe/London' (length=13)
    
    object(DateTime)[4]
      public 'date' => string '2012-02-01 11:40:10' (length=19)
      public 'timezone_type' => int 3
      public 'timezone' => string 'Europe/London' (length=13)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm seeing some very strange behaviour from the Apple Push Notification Servers when the
We're seeing a very strange issue when running the following application from a network
I'm seeing some very strange sorting behaviour using CaseInsensitiveComparer.DefaultInvariant. Words that start with a
I'm seeing something very very strange happening in a Flex app I'm maintaining. I've
I'm seeing a very strange issue with a .NET webservice being consumed by Flex.
I've been seeing this instruction as the very first line of numerous CSS files
I know there are other questions that are very similar to this, but their
I am seeing very, VERY strange behavior when I run certain reports: >> p
I am seeing a very strange problem on one of my production boxes. We
So I am seeing some very strange functionality in my app. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.