# 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
##### Share on other sites

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

##### Share on other sites

• 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
##### Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

## Join the conversation

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

Reply to this topic...

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.