简体   繁体   中英

How to get date when given a date and days from that date? php

I have to get a date in the future.

I have a date in the past and I need to add "X" days and get the resulting date.

Here is an example of what I want to do

2016-02-28 09:07:22 + 62 days = NewDate

I am using PHP

You have to use strtotime() function to convert the date string into seconds, then add the days, and then convert back with date() function if you want. The code will look like this:

$dateString = '2016-02-28 09:07:22';
$time = strtotime($dateString); //convert to seconds
$timeNew = $time + 62 * 24 * 60 * 60; //add 62 days in seconds
$dateNew = date("Y-m-d H:i:s", $timeNew); //convert back

If you are using the \\DateTime() object, you can do something like:

$past = new \DateTime('2016-02-28 09:07:22');
$interval = new \DateInterval('P64D'); // 64 days
$future = $past->add($interval);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM