php PHP日期;如何找到下一年?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3724368/
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
PHP date; How to find the next year?
提问by Testadmin
I want to get today's date + one year. How do I achieve this with PHP's date functions?
我想得到今天的日期 + 一年。如何使用 PHP 的日期函数实现这一点?
回答by gianebao
echo date('Y', strtotime('+1 year'));
回答by Gordon
$date = '2010-09-16';
echo date('Y-m-d', strtotime("+12 months $date"));
// 2011-09-16
On a sidenote: DateTime questions like this have been answered over and over again, so you could have found how to add to a date easily by using the search function.
旁注:像这样的 DateTime 问题已经被一遍又一遍地回答,因此您可以找到如何使用搜索功能轻松添加日期。
回答by fabrik
From PHP's documentation:
来自 PHP 的文档:
<?php
$date = new DateTime($your_supposed_date);
$date->add(new DateInterval('P1Y'));
echo $date->format('Y-m-d') . "\n";
?>
Gordon's much cleaner version (Thank you!):
戈登更干净的版本(谢谢!):
<?php
$date = new DateTime("+12 months $theDate");
echo $date->format('Y-m-d') . "\n";
?>
回答by Abdifatah Abdilahi
$Ad_year = 2015-10-20
<?php echo $Ad_year + 1?>
Result 2016
回答by Mariyo
The shortest version:
最短版本:
echo (int)date('Y') + 1;
回答by Flask
If you're working with timestamps
如果您正在使用时间戳
echo time()+60*60*24*365
回答by Fidi
You could use the new Datetime and Datetime_Intervall-classes introduced in the later PHP 5-versions.
您可以使用更高版本的 PHP 5 版本中引入的新 Datetime 和 Datetime_Intervall 类。
I once posted an answer in this question. Maybe it helps you :)
我曾经在这个问题中发布了一个答案。也许它可以帮助你:)
The advantage is, that this classes also checks for leap-seconds and leap-years, timezones, etc.
优点是,该类还检查闰秒和闰年、时区等。