简体   繁体   English

PHP 日期; 如何找到下一年?

[英]PHP date; How to find the next year?

I want to get today's date + one year.我想得到今天的日期+一年。 How do I achieve this with PHP's date functions?如何使用 PHP 的日期函数来实现这一点?

echo date('Y', strtotime('+1 year'));

You can use strtotime and date您可以使用strtotimedate

$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 问题已经被一遍又一遍地回答,因此您可以找到如何使用搜索功能轻松添加日期。

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";
?>
$Ad_year = 2015-10-20
<?php echo $Ad_year + 1?>
Result 2016

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.优点是,该类还检查闰秒和闰年、时区等。

如果您正在处理时间戳

echo time()+60*60*24*365

最短版本:

echo (int)date('Y') + 1;

最佳且简单的解决方案...您可以更改月份、年份或日期。

date('Y-m-d',strtotime("+1 day +2months +1 year"));

Below code also return next year from current date:下面的代码也从当前日期返回明年:

<?php echo date('Y', strtotime('+12 month'));>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM