PHP - add one week to a user-specified date

Most likely, this will be a duplicate for this question, but I'm struggling to find the exact answer to my problem.

The user enters the start date of the client’s lease (in the form on the previous page), then he needs to create the next date (in a week) that the client must pay. For instance:

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

Suppose the user entered in 2015/03/02:

 $start_date = "2015/03/02"; 

Then I want the date to be equal to the week of the week (2015/03/09):

 $date_to_pay = "2015/03/09"; 

How to get around this? Many thanks.

+5
source share
5 answers

You can try this

 $start_date = "2015/03/02"; $date = strtotime($start_date); $date = strtotime("+7 day", $date); echo date('Y/m/d', $date); 
+6
source

Try the following:

 date('dmY', strtotime('+1 week', $start_date)); 
+6
source

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 love it all at once:

 $date_to_pay = DateTime::createFromFormat('Y/m/d',$_POST['start_date']) ->add(DateInterval::createFromDateString('1 week')) ->format('Y/m/d'); 
+2
source
 $start_date = "2015/03/02"; $new_date= date("Y/m/d", strtotime("$start_date +1 week")); 
+1
source

You can use this.

 $startdate = $_POST['start_date']; $date_to_pay = date('Y/m/d',srtotime('+1 week',$startdate)); 
+1
source

Source: https://habr.com/ru/post/1214466/


All Articles