PHP - 将一周添加到用户定义的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28807774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:05:01  来源:igfitidea点击:

PHP - Add one week to a user defined date

phphtml

提问by jarnold

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:

假设用户在 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):

然后我希望支付的日期等于一周后(2015/03/09):

$date_to_pay = "2015/03/09";  

How would one go around doing this? Many thanks.

怎么做呢?非常感谢。

回答by priya786

You can try this

你可以试试这个

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

回答by Mihir Bhatt

Please try the following:

请尝试以下操作:

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

回答by Anthony

Object Oriented Style using DateTimeclasses:

使用DateTime类的面向对象风格:

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

回答by TECHNOMAN

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

回答by Narendrasingh Sisodia

You can use this:

你可以使用这个:

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