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

时间:2009-05-06 06:48:59

标签: php date date-arithmetic

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

我应该使用哪个日期函数?

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

6 个答案:

答案 0 :(得分:34)

使用strtotime。

$newDate = strtotime('+15 days',$date)
<$> $ newDate现在是$ date之后的15天。 $ date是unix时间。

http://uk.php.net/strtotime

答案 1 :(得分:17)

试试这个

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

答案 2 :(得分:14)

自PHP 5.2.0起,DateTime类内置可用

$date = new DateTime($postedDate);

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

echo $date->format('Y-m-d');

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

答案 3 :(得分:9)

$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

答案 4 :(得分:3)

这很简单;试试这个:

$date = "2013-06-12"; // date you want to upgade

echo $date = date("Y-m-d", strtotime($date ." +1 day") );

答案 5 :(得分:2)

无论如何输入格式是什么?

1)如果您的日期是年,月,日数组,那么您可以mktime(0,0,0,$ month,$ day + 15,$ year)或mktime(0,0,0) ,$ month + 1,$ day,$ year)。请注意,mktime是一个智能函数,可以正确处理越界值,因此mktime(0,0,0,13,33,2008)(即2008年第13个月的第13天)将返回2月的时间戳,2009年2月。

2)如果您的日期是时间戳,那么您只需添加15 * SECONDS_IN_A_DAY,然后输出日期(/ *任何格式* /,$ postedDate)。如果你需要添加一个月30天当然不会一直正常工作,所以你可以先将时间戳转换为月,日和年(用date()函数),然后使用(1)。

3)如果你的日期是一个字符串,你首先用strtotime()解析它,然后你喜欢它。