php 添加 30 天至今
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37649339/
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 30 days to date
提问by Rahul Mukati
Hello all i am trying to add 30 days to my date. I am using below coding.
大家好,我想为我的约会增加 30 天。我正在使用以下编码。
<?php
$next_due_date = date('05/06/2016', strtotime("+30 days"));
echo $next_due_date;
?>
But it is returning to "05/06/2016" only!
但它只返回“05/06/2016”!
Please help me!
请帮我!
回答by zanderwar
Do notuse php's date() function, it's not as accurate as the below solution and furthermore it is unreliable in the future.
千万不能使用PHP的date()函数,它不是那么精确以下解决方案,而且它在未来的不可靠。
Use the DateTimeclass
使用DateTime类
<?php
$date = new DateTime('2016-06-06'); // Y-m-d
$date->add(new DateInterval('P30D'));
echo $date->format('Y-m-d') . "\n";
?>
The reason you should avoid anything to do with UNIX timestamps (time()
, date()
, strtotime()
etc) is that they will inevitably break in the year 2038due to integer limitations.
你应该避免任何与UNIX的原因时间戳(time()
,date()
,strtotime()
等),是他们将不可避免地在今年突破2038由于整数限制。
The maximum value of an integer is 2147483647
which converts to Tuesday, 19 January 2038 03:14:07
so come this time; this minute; this second; everything breaks
整数的最大值是2147483647
这次转换成Tuesday, 19 January 2038 03:14:07
这样;这一刻;这一秒;一切都坏了
Another example of why I stick to using DateTime is that it's actually able to calculate months correctly regardless of what the current date is:
我坚持使用 DateTime 的另一个例子是,无论当前日期是什么,它实际上都能够正确计算月份:
$now = strtotime('31 December 2019');
for ($i = 1; $i <= 6; $i++) {
echo date('d M y', strtotime('-' . $i .' month', $now)) . PHP_EOL;
}
You'd get the following sequence of dates:
您将获得以下日期序列:
31 December
31 November
31 October
31 September
31 August
31 July
31 June
PHP conveniently recognises that three of these dates are illegal and converts them into its best guess, leaving you with:
PHP 方便地识别出其中三个日期是非法的,并将它们转换为最佳猜测,从而为您提供:
01 Dec 19
31 Oct 19
01 Oct 19
31 Aug 19
31 Jul 19
01 Jul 19
回答by phreakv6
Please try this.
请试试这个。
echo date('m/d/Y',strtotime('+30 days',strtotime('05/06/2016'))) . PHP_EOL;
This will return 06/06/2016
. Am assuming your initial date was in m/d/Y format. If not, fret not and use this.
这将返回06/06/2016
。我假设您的初始日期是 m/d/Y 格式。如果没有,请不要担心并使用它。
echo date('d/m/Y',strtotime('+30 days',strtotime(str_replace('/', '-', '05/06/2016')))) . PHP_EOL;
This will give you the date in d/m/Y format while also assuming your initial date was in d/m/Y format. Returns 05/07/2016
这将为您提供 d/m/Y 格式的日期,同时还假设您的初始日期为 d/m/Y 格式。退货05/07/2016
If the input date is going to be in mysql, you can perform this function on mysql directly, like this.
如果输入日期要在mysql中,你可以直接在mysql上执行这个功能,就像这样。
DATE_ADD(due_date, INTERVAL 1 MONTH);
回答by Hassaan
You need to provide date format like d/m/Y
instead of 05/06/2016
您需要提供日期格式,d/m/Y
而不是05/06/2016
Try
尝试
$old_date = '05-06-2016';
$next_due_date = date('d-m-Y', strtotime($old_date. ' +30 days'));
echo $next_due_date;
回答by chris85
The first parameter is the format, not the current date.
第一个参数是格式,而不是当前日期。
<?php
$next_due_date = date('d/m/Y', strtotime("+30 days"));
echo $next_due_date;
Demo: https://eval.in/583697
If you want to add the 30 days
to a particular starting date use the second parameter of the strtotime
function to tell it where to start.
如果您想将30 days
加到特定的开始日期,请使用strtotime
函数的第二个参数来告诉它从哪里开始。
When in doubt about how a function works refer to the manual.
当对功能的工作方式有疑问时,请参阅手册。
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.date.php
回答by Prasad
$date = "1998-08-14";
$newdate = strtotime ( '30 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate;