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 7774443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:22:50+00:00 2026-06-01T17:22:50+00:00

>> $start_dt = new DateTime() DateTime::__set_state(array( ‘date’ => ‘2012-04-11 08:34:01’, ‘timezone_type’ => 3, ‘timezone’

  • 0
>> $start_dt = new DateTime()
DateTime::__set_state(array(
   'date' => '2012-04-11 08:34:01',
   'timezone_type' => 3,
   'timezone' => 'America/Los_Angeles',
))
>> $end_dt = new DateTime()
DateTime::__set_state(array(
   'date' => '2012-04-11 08:34:06',
   'timezone_type' => 3,
   'timezone' => 'America/Los_Angeles',
))
>> $start_dt->setTimestamp(strtotime('31-Jan-2012'))
DateTime::__set_state(array(
   'date' => '2012-01-31 00:00:00',
   'timezone_type' => 3,
   'timezone' => 'America/Los_Angeles',
))
>> $end_dt->setTimestamp(strtotime('1-Mar-2012'))
DateTime::__set_state(array(
   'date' => '2012-03-01 00:00:00',
   'timezone_type' => 3,
   'timezone' => 'America/Los_Angeles',
))
>> $interval = $start_dt->diff($end_dt)
DateInterval::__set_state(array(
   'y' => 0,
   'm' => 0,
   'd' => 30,
   'h' => 0,
   'i' => 0,
   's' => 0,
   'invert' => 0,
   'days' => 30,
))
>> $interval->format('%mm %dd')
'0m 30d'

i.e., 31-Jan-2012 to 1-Mar-2012 yields less than a month! I’d expect the output to be 1 month, 1 day. It shouldn’t matter the number of days in February; that’s the point of using a time library — it’s supposed to handle these things. WolframAlpha agrees.

Should I file a bug to PHP? Is there a hack/fix/workaround to get months to work as expected?

  • 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-06-01T17:22:51+00:00Added an answer on June 1, 2026 at 5:22 pm

    Should I file a bug to PHP?

    No.

    The “month” part of the interval means that the month part of the start date can be incremented by that many months. The behaviour in PHP, taking your start date of 31-Jan-2012 and incrementing the month (literally, 31-Feb-2012) and then correcting for a valid date (PHP does this for you) would give 02-Mar-2012 which is later than the target date that you are working with.

    To demonstrate this, take your start date and add n months for a few months to see the behaviour.

    31-Jan-2012 (Interval)
    02-Mar-2012 (P1M)
    31-Mar-2012 (P2M)
    01-May-2012 (P3M)
    31-May-2012 (P4M)
    01-Jul-2012 (P5M)
    

    You can see that the month is being incremented, then adjusted to make a valid date.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code: import datetime as dt import scikits.timeseries as ts ts_start_datetime= ts.Date(freq='T',year=2011,month=1,day=1,hour= 0,minute=0) ts_end_datetime
I've got a sorted list of datetimes: (with day gaps) list_of_dts = [ datetime.datetime(2012,1,1,0,0,0),
I start out with date strings: from operator import itemgetter import datetime as DT
Start a new Silverlight application... and in the code behind (in the Loaded event),
That's my code inside my MainPage.xaml.cs public MainPage() { InitializeComponent(); DispatcherTimer dt = new
I'm using php with smarty. In php I have two arrays: $code = Array
I couldn't find best solution when DateTime value is NULL. I use this techinque
I'll start by saying I am pretty new to unit testing and I'd like
I have an array that I want to pass to a javascript onclick function.
I am trying to display a nullable date time in my JSON response. In

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.