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

The Archive Base Latest Questions

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

i was trying to compare the difference between 2 dates, but it seems the

  • 0

i was trying to compare the difference between 2 dates, but it seems the results are pretty wrong, for example this code:

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days')."<br />";

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-15');
$interval2 = $datetime1->diff($datetime2);
echo $interval2->format('%R%a days')."<br />";

if($interval == $interval2){ echo "true"; }else{echo "false"; }

Returns true, but above you can see the date differences are not the same, in fact echo prints +2 and +4. Any idea in how to compare 2 date differences?

EDIT: the datetime::diff returns a dateinterval object, in fact it doesn’t implement comparison operators, https://bugs.php.net/bug.php?id=49914
I’ll use dateinterval vars to check the difference, thanks for the answers

  • 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:23:48+00:00Added an answer on May 28, 2026 at 12:23 am

    It seems that DateInterval doesn’t implement a comparison function internally. Extensions are allowed to define custom comparison rules for their predefined classes. Evidently it falls back to a loose comparison that the objects are of the same class.

    This feature request provides a patch to add this functionality in, but it doesn’t seem to have made it into the source at any point.

    To get around this issue, you can either compare each member variable of your objects yourself (years, months etc.) or you can cast each object to an array:

    if ((array) $interval == (array) $interval2) {
        echo 'true';
    } else {
        echo 'false';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to compare these two chars but on win 32 Visual Studio 2008:
I've been trying to compare the memory footprint between a VB6 application and .Net
I am trying to compare two strings in Smalltalk, but I seem to be
I'm trying to get a feel for the difference in performance between integer multiplication
I am trying to find the difference between the following two sets: A =
I trying to write a programm for file compare. For example: file1 1 2
I am trying to compare performance between raw pointers, boost shared_ptr and boost weak_ptr.
I am trying to compare two dates which are in Finnish time form like
I am trying to find the difference between two generic lists, as in the
I'm trying to understand the main difference between returning false from a statement as

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.