23

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

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

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

4

6 回答 6

35

使用 strtotime。

$newDate = strtotime('+15 days',$date)

$newDate 现在将在 $date 之后 15 天。$date 是 unix 时间。

http://uk.php.net/strtotime

于 2009-05-06T07:22:16.013 回答
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");
于 2009-05-06T06:52:54.603 回答
15

从 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

于 2012-11-25T19:49:23.140 回答
11
$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
于 2013-05-23T13:12:35.030 回答
4

这很简单;试试这个:

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

echo $date = date("Y-m-d", strtotime($date ." +1 day") );
于 2013-05-22T17:50:50.397 回答
3

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

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

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

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

于 2009-05-06T08:21:36.043 回答