[英]add one year to datetime with php
$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 亚当拉马丹
strtotime()
是你正在寻找的功能:
$data['user']['seal_data'] = date('Y-m-d H:i:s', strtotime('+1 year', strtotime($data['user']['time'])));
First, you have to convert the MySQL datetime
to something that PHP can understand. 首先,您必须将MySQL
datetime
时间转换为PHP可以理解的内容。 There are two ways of doing this... 有两种方法可以做到这一点......
Use UNIX_TIMESTAMP()
in your query to tell MySQL to return a UNIX timestamp of the datetime
column. 在查询中使用
UNIX_TIMESTAMP()
告诉MySQL返回datetime
列的UNIX时间戳。
SELECT whatever, UNIX_TIMESTAMP(myTime) AS 'myUnixTime' FROM myTable;
Use DateTime::createFromFormat
to convert your string time to something PHP can understand. 使用
DateTime::createFromFormat
将您的字符串时间转换为PHP可以理解的内容。
$date = DateTime::createFromFormat('Ymd 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: 如果您有unix时间戳,则可以使用以下内容添加一年:
$inAYear = strtotime('+1 year', $data['user']['unixTime']);
If you have a DateTime
object, you can use the following: 如果您有
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: 如果您有unix时间戳,则可以使用以下内容:
$strTime = date('Ymd H:i:s', $inAYear);
If you have a DateTime
object, you can use the following: 如果您有
DateTime
对象,则可以使用以下内容:
$strTime = $inAYear->format('Ymd 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;
This question is top on a google search for "php datetime add one year", but severely outdated . 这个问题是谷歌搜索“php datetime add one year”的顶级问题,但严重过时了 。 While most of the previous answers will work fine for most cases, the established standard is to use DateTime objects for this instead, primarily due
strtotime
requiring careful manipulation of timezones and DST. 虽然大多数以前的答案将正常工作在大多数情况下,所建立的标准是使用datetime对象为这个代替,这主要是由于
strtotime
需要时区和夏令时的谨慎操作。
$date = new DateTime('2011-03-07 00:33:45', [user TZ]);
$date = new DateTime('2011-03-07 00:33:45', [user TZ]);
$date->modify('+1 year');
$date->modify('+1 year');
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Ymd H:i:s');
echo $date->format('Ymd H:i:s');
Following this pattern for manipulating dates and times will handle the worst oddities of timezone/DST/leap-time for you. 遵循这种操作日期和时间的模式将为您处理最糟糕的时区/ DST /跳跃时间。
Just remember two final notes: 记住最后两个笔记:
How to calculate the difference between two dates using PHP? 如何使用PHP计算两个日期之间的差异?
Convert date format yyyy-mm-dd => dd-mm-yyyy 转换日期格式yyyy-mm-dd => dd-mm-yyyy
I think you could use strtotime() to do this pretty easily. 我想你可以使用strtotime()很容易地做到这一点。 Something like:
就像是:
$newdata = date('c', strtotime($data['user']['time'] . ' +1 year'));
Though the 'c' format string isn't the same as your input format. 虽然'c'格式字符串与输入格式不同。 You could consult date() 's docs for how to construct the correct one.
您可以参考date()的文档来了解如何构造正确的文档。
'Ymd H:i:s' — as Tim Cooper suggests — looks correct. 'Ymd H:我:' - 正如蒂姆库珀建议的那样 - 看起来是正确的。
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"));
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 . 要添加时间/日/月/年,请不要使用
strtotime()
函数,因为它无法添加超过2038年的时间。 So here I would prefer to use DateTime()
function. 所以在这里我更喜欢使用
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.