如何在PHP 15天/ 1个月后获得日期?

Shy*_*yju 23 php date date-arithmetic

在我的PHP代码中,我的变量"$ postedDate"中有一个日期.
现在我希望在7天,15天,1个月和2个月过后获得日期.

我应该使用哪个日期功能?

输出日期格式应为美国格式.

Pau*_*ams 35

使用strtotime.

$newDate = strtotime('+15 days',$date)
Run Code Online (Sandbox Code Playgroud)

$ newDate现在是$ date之后的15天.$ date是unix时间.

http://uk.php.net/strtotime


Sad*_*egh 18

试试这个

$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");
Run Code Online (Sandbox Code Playgroud)


Lam*_*amy 15

从PHP 5.2.0开始DateTime,类中的构建可用

$date = new DateTime($postedDate);

$date->modify('+1 day');

echo $date->format('Y-m-d');
Run Code Online (Sandbox Code Playgroud)

http://php.net/manual/en/class.datetime.php


ARJ*_* KP 10

$date=strtotime(date('Y-m-d'));  // if today :2013-05-23

$newDate = date('Y-m-d',strtotime('+15 days',$date));

echo $newDate; //after15 days  :2013-06-07

$newDate = date('Y-m-d',strtotime('+1 month',$date));

echo $newDate; // after 1 month :2013-06-23
Run Code Online (Sandbox Code Playgroud)