在PHP中添加天数

时间:2022-08-25 18:26:15

Is there any php function available where I can add days to a date to make up another date? For example, I have a date in the following format: 27-December-2011

有什么php函数可以让我在一个日期上增加天数来弥补另一个日期吗?例如,我有以下格式的日期:12月27日至2011年12月27日

If I add 7 to the above, it should give: 03-January-2012.

如果上面加7,应该是:03- 01 -2012。

Many thanks

非常感谢

7 个解决方案

#1


15  

Try this

试试这个

$add_days = 7;
$date = date('Y-m-d',strtotime($date) + (24*3600*$add_days));

#2


8  

Look at this simple snippet

看看这个简单的片段

$date = date("Y-m-d");// current date

$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");

#3


5  

You can use the add method of DateTime. Anyway this solution works for php version >= 5.3

您可以使用DateTime的add方法。无论如何,这个解决方案适用于php版本>= 5.3

#4


5  

date('Y-m-d', strtotime('+6 days', strtotime($original_date)));

#5


2  

Actually it's easier than all that.

实际上比这一切都简单。

$some_var = date("Y-m-d",strtotime("+7 day"))

You can use a variable instead of the string, of course. It will be great if the people answering the questions, won't complicate things. Less code, means less time to waste on the server ;).

当然,您可以使用变量而不是字符串。如果人们回答这些问题会很好,不会让事情变得复杂。更少的代码意味着更少的时间浪费在服务器上;)

#6


1  

$date = new DateTime('27-December-2011');
$date->add(new DateInterval('P7D'));
echo $date->format('d-F-Y') . "\n";

Change the format string to be whatever you want. (See the documentation for date()).

将格式字符串更改为您想要的格式。(参见日期()的文档)。

#7


0  

$registered = $udata->user_registered;
$registered = date( "d m Y", strtotime( $registered ));
$challanexpiry = explode(' ', $registered);
$day   = $challanexpiry[0];
$month = $challanexpiry[1];
$year  = $challanexpiry[2];
$day = $day+10;
$bankchallanexpiry = $day . " " . $month . " " . $year;

#1


15  

Try this

试试这个

$add_days = 7;
$date = date('Y-m-d',strtotime($date) + (24*3600*$add_days));

#2


8  

Look at this simple snippet

看看这个简单的片段

$date = date("Y-m-d");// current date

$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");

#3


5  

You can use the add method of DateTime. Anyway this solution works for php version >= 5.3

您可以使用DateTime的add方法。无论如何,这个解决方案适用于php版本>= 5.3

#4


5  

date('Y-m-d', strtotime('+6 days', strtotime($original_date)));

#5


2  

Actually it's easier than all that.

实际上比这一切都简单。

$some_var = date("Y-m-d",strtotime("+7 day"))

You can use a variable instead of the string, of course. It will be great if the people answering the questions, won't complicate things. Less code, means less time to waste on the server ;).

当然,您可以使用变量而不是字符串。如果人们回答这些问题会很好,不会让事情变得复杂。更少的代码意味着更少的时间浪费在服务器上;)

#6


1  

$date = new DateTime('27-December-2011');
$date->add(new DateInterval('P7D'));
echo $date->format('d-F-Y') . "\n";

Change the format string to be whatever you want. (See the documentation for date()).

将格式字符串更改为您想要的格式。(参见日期()的文档)。

#7


0  

$registered = $udata->user_registered;
$registered = date( "d m Y", strtotime( $registered ));
$challanexpiry = explode(' ', $registered);
$day   = $challanexpiry[0];
$month = $challanexpiry[1];
$year  = $challanexpiry[2];
$day = $day+10;
$bankchallanexpiry = $day . " " . $month . " " . $year;