使用 php 将一年添加到日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5212240/
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
add one year to datetime with php
提问by Adam Ramadhan
$data['user']['time'] = '2011-03-07 00:33:45';
how can we add 1 year to this date ?
我们如何在这个日期上加上 1 年?
something like $newdata = $data['user']['time'] + 1 year
?
像$newdata = $data['user']['time'] + 1 year
什么?
or
或者
$newdata = 2012-03-07 00:33:45
Thanks
谢谢
Adam Ramadhan
亚当·拉马丹
回答by Tim Cooper
strtotime()
is the function you're looking for:
strtotime()
是您正在寻找的功能:
$data['user']['seal_data'] = date('Y-m-d H:i:s', strtotime('+1 year', strtotime($data['user']['time'])));
回答by Andrew Moore
First, you have to convert the MySQL datetime
to something that PHP can understand. There are two ways of doing this...
首先,您必须将 MySQL 转换为datetime
PHP 可以理解的内容。有两种方法可以做到这一点......
Use
UNIX_TIMESTAMP()
in your query to tell MySQL to return a UNIX timestamp of thedatetime
column.SELECT whatever, UNIX_TIMESTAMP(myTime) AS 'myUnixTime' FROM myTable;
Use
DateTime::createFromFormat
to convert your string time to something PHP can understand.$date = DateTime::createFromFormat('Y-m-d H:i:s', $data['user']['time']);
使用
UNIX_TIMESTAMP()
在查询中让MySQL返回的UNIX时间戳datetime
列。SELECT whatever, UNIX_TIMESTAMP(myTime) AS 'myUnixTime' FROM myTable;
使用
DateTime::createFromFormat
你的字符串时间转换为PHP的东西可以理解。$date = DateTime::createFromFormat('Y-m-d H:i:s', $data['user']['time']);
Once that is done, you can work with the time... Depending on the method you used above, you can use one of the following.
完成后,您可以使用时间...根据您在上面使用的方法,您可以使用以下方法之一。
If you have a unix timestamp, you can use the following to add a year:
$inAYear = strtotime('+1 year', $data['user']['unixTime']);
If you have a
DateTime
object, you can use the following:$inAYear = $date->add(new DateInterval('P1Y'));
如果您有 unix 时间戳,则可以使用以下命令添加年份:
$inAYear = strtotime('+1 year', $data['user']['unixTime']);
如果您有
DateTime
对象,则可以使用以下内容:$inAYear = $date->add(new DateInterval('P1Y'));
Now, to display your date in a format that is respectable, you must tell PHP to return a string in the proper format.
现在,要以一种受人尊敬的格式显示您的日期,您必须告诉 PHP 以正确的格式返回一个字符串。
If you have a unix timestamp, you can use the following:
$strTime = date('Y-m-d H:i:s', $inAYear);
If you have a
DateTime
object, you can use the following:$strTime = $inAYear->format('Y-m-d H:i:s');
如果您有 unix 时间戳,则可以使用以下内容:
$strTime = date('Y-m-d H:i:s', $inAYear);
如果您有
DateTime
对象,则可以使用以下内容:$strTime = $inAYear->format('Y-m-d H:i:s');
Alternatively, if you don't want to deal with all of that, you can simply add one year when you query.
或者,如果您不想处理所有这些,您可以在查询时简单地添加一年。
SELECT whatever, DATE_ADD(myTime, INTERVAL 1 YEAR) AS 'inAYear' FROM myTable;
回答by Tony Chiboucas
Current (2017) Practice is to use DateTime
Current (2017) 实践是使用DateTime
This question is top on a google search for "php datetime add one year", but severely outdated. While most of the previous answers will work fine for most cases, the established standard is to use DateTimeobjects for this instead, primarily due strtotime
requiring careful manipulation of timezones and DST.
这个问题是谷歌搜索“php datetime add one year”的首要问题,但已经严重过时了。虽然以前的大多数答案在大多数情况下都可以正常工作,但已建立的标准是为此使用DateTime对象,主要是因为strtotime
需要仔细操作时区和 DST。
TL;DR
TL; 博士
- Convert to DateTime:
$date = new DateTime('2011-03-07 00:33:45', [user TZ]);
- Use DateTime::modify:
$date->modify('+1 year');
- Format to needs.
- Change the timezone with DateTime::setTimezonefrom the list of supported timezones:
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
- Convert to string with DateTime::format:
echo $date->format('Y-m-d H:i:s');
- Change the timezone with DateTime::setTimezonefrom the list of supported timezones:
- 转换为日期时间:
$date = new DateTime('2011-03-07 00:33:45', [user TZ]);
- 使用DateTime::modify:
$date->modify('+1 year');
- 需要的格式。
- 从支持的时区列表中使用DateTime::setTimezone更改时区:
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
- 使用DateTime::format转换为字符串:
echo $date->format('Y-m-d H:i:s');
- 从支持的时区列表中使用DateTime::setTimezone更改时区:
Following this pattern for manipulating dates and times will handle the worst oddities of timezone/DST/leap-time for you.
遵循这种操作日期和时间的模式将为您处理最糟糕的时区/夏令时/闰时。
Just remember two final notes:
请记住最后两个注意事项:
- Life is easier with your system timezone set at UTC.
- NEVER modify the system timezone outside of configuration files.
- I've seen too much code that relies on date_default_timezone_set. If you're doing this, stop. Save the timezone in a variable, and pass it around your application instead, please.
- 系统时区设置为UTC ,生活更轻松。
- 切勿在配置文件之外修改系统时区。
- 我见过太多依赖date_default_timezone_set 的代码。如果你正在这样做,停止。将时区保存在一个变量中,并将其传递给您的应用程序,请。
More Reading
更多阅读
How to calculate the difference between two dates using PHP?
Convert date format yyyy-mm-dd => dd-mm-yyyy
转换日期格式 yyyy-mm-dd => dd-mm-yyyy
回答by drewish
I think you could use strtotime()to do this pretty easily. Something like:
我认为您可以使用strtotime()轻松完成此操作。就像是:
$newdata = date('c', strtotime($data['user']['time'] . ' +1 year'));
Though the 'c' format string isn't the same as your input format. You could consult date()'s docs for how to construct the correct one.
尽管 'c' 格式字符串与您的输入格式不同。您可以查阅date()的文档以了解如何构建正确的文档。
'Y-m-d H:i:s' — as Tim Cooper suggests — looks correct.
'Ymd H:i:s' - 正如 Tim Cooper 所建议的 - 看起来是正确的。
回答by Damiqib
This should do the trick (not tested).
这应该可以解决问题(未测试)。
$data = "2011-03-07 00:33:45";
echo 'Original date +1 year: ' . date('Y-m-d H:i:s', strtotime(date("Y-m-d H:i:s", strtotime($data)) . " +1 year"));
回答by Pran
First-of-all if your date format is separated by a slash (/), like '2019/12/31'then you should convert it in dash (-) format, like '2019-12-31', to do so use str_replace()
function.
首先,如果您的日期格式由斜杠 (/) 分隔,例如“2019/12/31”,那么您应该将其转换为破折号 (-) 格式,例如“ 2019-12-31”,以这样做使用str_replace()
功能。
$string = str_replace('/', '-', '2019/12/31'); //output: 2019-12-31
To add time/day/month/year do not use strtotime()
function, because it can't add a time which is beyond year 2038.
So here I would prefer to use DateTime()
function.
添加时间/日/月/年不要使用strtotime()
函数,因为它不能添加超过2038 年的时间。所以在这里我更喜欢使用DateTime()
函数。
$string = '2000-01-01';
$date = new DateTime($string);
$date->add(new DateInterval('P60Y5M2DT6H3M25S')); //60 Years 5 Months 2 Days 6 Hours 3 Minutes 25 Seconds
echo $date->format('Y-m-d H:i:s'); //output: 2060-06-03 06:03:25