如何在 PHP 中获得 15 天/1 个月后的日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/828332/
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
How can I get a date after 15 days/1 month in PHP?
提问by Shyju
In my PHP code I have a date in my variable "$postedDate".
Now I want to get the date after 7 days, 15 days, one month and 2 months have elapsed.
在我的 PHP 代码中,我的变量“$postedDate”中有一个日期。
现在我想在 7 天、15 天、1 个月和 2 个月过去后获取日期。
Which date function should I use?
我应该使用哪个日期函数?
Output date format should be in US format.
输出日期格式应为美国格式。
回答by PaulJWilliams
Use strtotime.
使用 strtotime。
$newDate = strtotime('+15 days',$date)
$newDate will now be 15 days after $date. $date is unix time.
$newDate 现在将是 $date 之后的 15 天。$date 是 unix 时间。
回答by Sadegh
try this
尝试这个
$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");
回答by Lamy
Since PHP 5.2.0 the DateTimebuild in class is available
自 PHP 5.2.0 起,DateTime内置类可用
$date = new DateTime($postedDate);
$date->modify('+1 day');
echo $date->format('Y-m-d');
回答by ARJUN KP
$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
回答by Siddhartha Dutta
This is very simple; try this:
这很简单;尝试这个:
$date = "2013-06-12"; // date you want to upgade
echo $date = date("Y-m-d", strtotime($date ." +1 day") );
回答by Ilya Birman
What's the input format anyway?
无论如何输入格式是什么?
1) If your date is, say, array of year, month and day, then you can mktime (0, 0, 0, $month, $day + 15, $year) or mktime (0, 0, 0, $month + 1, $day, $year). Note that mktime is a smart function, that will handle out-of-bounds values properly, so mktime (0, 0, 0, 13, 33, 2008) (which is month 13, day 33 of 2008) will return timestamp for February, 2, 2009.
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 个月,第 33 天)将返回二月的时间戳, 2, 2009。
2) If your date is a timestamp, then you just add, like, 15*SECONDS_IN_A_DAY, and then output that with date (/* any format */, $postedDate). If you need to add one month 30 days won't of course always work right, so you can first convert timestamp to month, day and year (with date () function) and then use (1).
2) 如果您的日期是时间戳,那么您只需添加 15*SECONDS_IN_A_DAY,然后将其与日期一起输出(/* 任何格式 */,$postedDate)。如果您需要添加一个月 30 天当然不会总是正常工作,因此您可以先将时间戳转换为月、日和年(使用 date() 函数),然后使用(1)。
3) If your date is a string, you first parse it, for example, with strtotime (), then do whatevee you like.
3)如果你的日期是字符串,你先解析它,比如用strtotime(),然后做你喜欢的。

