Jump to content

A small issue with finding time difference between 2 dates and then combining the difference with new time.


Go to solution Solved by imgrooot,

Recommended Posts


// find the time difference between now and the expiry date
$expiry_date = '2017-04-20 09:02:23';
$then = new DateTime($expiry_date);
$now = new DateTime();
$remaining_time = $then->diff($now);

// I want to add two weeks to the new date.
$two_weeks = $now->add(new DateInterval('P2W'))->format('Y-m-d H:i:s');

// new date that combines the remaining time from above and adds 2 weeks to create a new date. This is the issue here. How do I combine these two dates properly? The new date has to be in this format ('Y-m-d H:i:s')
$new_date = $remaining_time + $two_weeks;

 

Edited by imgrooot
  • Solution

You want now + difference between now and then + two weeks? Isn't that the same as then + two weeks?

 

Now that I think about it, I guess your right.

 

Anyways I have solved the issue already. Here it is.

// find the time difference between now and the expiry date
      $expiry_date      = '2017-04-20 09:02:23';
      $then             = new DateTime($expiry_date);
      $now              = new DateTime();
      $remaining_days   = $then->diff($now)->format("%a");
      
      // I want to add two weeks to the new date.
      $current          = new DateTime();
      $two_weeks        = new DateInterval('P2W');
      $total_date       = $current->add($two_weeks)->format('Y-m-d H:i:s');

      // current date + 2 weeks + remaining days difference
      $dateTime = new DateTime($total_date);
      $new_date = $dateTime->modify('+'.$remaining_days.' days');


      echo $new_date->format('Y-m-d H:i:s');
Edited by imgrooot
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.