Jump to content

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


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;

 

Link to comment
Share on other sites

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');
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.