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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:59:22+00:00 2026-06-05T20:59:22+00:00

I have a question regarding the proper way to modify a php DateTime object.

  • 0

I have a question regarding the proper way to modify a php DateTime object. Currently, I’m doing something like:

$origEvent = new DateTime(...);
$newEvent = new DateTime(...);
$someOtherEvent = new DateTime(...);

//get the time difference between the orignal event and the edited event
$diff = $origEvent->diff($newEvent);

$someOtherEvent->add($diff);

Using the DateTime::add() method seems to work whether we are adding or subtracting time from $someOtherEvent. Is this the correct way to do this? I know there is DateTime::sub() used for subtracting time, but it seems that as long as the DateInterval (produced by the $origEvent->diff()) has the inverted flag, the DateTime::add() knows to actually subtract time. Is this correct? Should I be using something like DateTime::modify?

  • 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-05T20:59:23+00:00Added an answer on June 5, 2026 at 8:59 pm

    Actually, DateTime::diff() returns the difference between two DateTime objects in a relative manner. Thus :

    • If date1 is lower than date2 then date1->diff(date2) will result in a negative interval
    • If date2 is lower than date1 then date1->diff(date2) will result in a positive interval

    So :

    • The use of DateTime::add() with a negative interval is equivalent to the use of DateTime::sub() with the absolute value of this same interval
    • The use of DateTime::sub() with a negative interval is equivalent to the use of DateTime::add() with the absolute value of this same interval

    For reasons of clarity of the code you can set the second parameter of DateTime::diff() to true in order to get an absolute difference every time and then choose the best function between DateTime::add() and DateTime::sub() knowing that you have an absolute difference.

    Some words about the choice between DateTime::modify() and DateTime::add(), DateTime:sub() :

    • As long as you are using DateTime::diff() you are assuming that your PHP version is greater than or equal to 5.3.0, and you can use all new functions available in this PHP version; that is to say DateTime::add() and DateTime::sub()
    • The use of DateTime::modify() is justified when your PHP version is greater than or equal to 5.2.0. (In this version DateTime::add() and DateTime::sub() are not yet available)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a quick question regarding the proper way to access Javascript class member
I have a question regarding the proper workflow of git in a PHP/MySQL workflow.
I have a question regarding static function in php. let's assume that I have
I have a question regarding the method i am using for doing Business Rule
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have
I have question regarding disabling browser caching. I have already found few solutions, and
I have a question regarding dependency injection. say i want to create a class
I have a question regarding the onkeypress event on JavaScript. Is it possible to
I have a question regarding the integration of business objects developed using Linq To

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.