PHP - Add one week to a user defined date [duplica

2020-08-13 11:26发布

问题:

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.

回答1:

You can try this

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


回答2:

Please try the following:

date('d.m.Y', strtotime('+1 week', strtotime($start_date)));


回答3:

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');


回答4:

$start_date = "2015/03/02";  
$new_date= date("Y/m/d", strtotime("$start_date +1 week"));


回答5:

You can use this:

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