15

There's more than likely going to be a duplicate for this question, but I'm struggling to find a precise answer for my problem.

The user enters a starting date for a client's rent (on a form on a previous page), then it needs to generate the next date (one week later) that the client is required to pay. For example:

$start_date = $_POST['start_date'];  
$date_to_pay = ???  

Lets say the user enters in 2015/03/02:

$start_date = "2015/03/02";  

I then want the date to pay to be equal to a week later (2015/03/09):

$date_to_pay = "2015/03/09";  

How would one go around doing this? Many thanks.

jarnold
  • 205
  • 1
  • 2
  • 12
  • 2
    Yes! There are many, many duplicates: `$_POST['start_date'] = '2015/03/02'; $date = new DateTime($_POST['start_date']); $date->add(new DateInterval('P1W')); echo $date->format('Y/m/d');` – Mark Baker Mar 02 '15 at 10:44
  • create date time from user input then add a week on it. For code see @MarkBaker 's comment – Nabin Kunwar Mar 02 '15 at 10:44
  • check this link : http://stackoverflow.com/questions/6086389/php-date-format-yyyy-mm-dd-minus-or-add-one-week-from-now – jems Mar 02 '15 at 10:47

5 Answers5

25

You can try this

$start_date = "2015/03/02";  
$date = strtotime($start_date);
$date = strtotime("+7 day", $date);
echo date('Y/m/d', $date);
priya786
  • 1,804
  • 12
  • 11
  • 1
    Sidenote: + 7 days is nice, but you could also just do + 1 week, just personnal preference.. – Naruto Mar 02 '15 at 10:52
  • 3
    One should note that this is a very poor method of doing this, `strtotime` returns seconds since epoch, you can simply add one week in seconds to the time and avoid the extra slow call to `strtotime`. `$date = strtotime($start_date) + 604800;` – HostFission Sep 13 '17 at 08:44
15

Please try the following:

date('d.m.Y', strtotime('+1 week', strtotime($start_date)));
Mihir Bhatt
  • 3,019
  • 2
  • 37
  • 41
10

Object Oriented Style using DateTime classes:

$start_date = DateTime::createFromFormat('Y/m/d', $_POST['start_date']);

$one_week = DateInterval::createFromDateString('1 week');

$start_date->add($one_week);

$date_to_pay = $start_date->format('Y/m/d');

Or for those who like to have it all in one go:

$date_to_pay = DateTime::createFromFormat('Y/m/d',$_POST['start_date'])
                       ->add(DateInterval::createFromDateString('1 week'))
                       ->format('Y/m/d');
Anthony
  • 36,459
  • 25
  • 97
  • 163
2
$start_date = "2015/03/02";  
$new_date= date("Y/m/d", strtotime("$start_date +1 week"));
TECHNOMAN
  • 361
  • 1
  • 9
2

You can use this:

$startdate = $_POST['start_date'];
$date_to_pay = date('Y/m/d',strtotime('+1 week',$startdate));
mochaMame
  • 57
  • 9
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54