php - 增加天数的问题
php - issue in increase days-date
我尝试在 m-d-Y 中增加天数 (60),但计算错误。它被认为是 d-m-y 格式。
echo "Today is :: ".date("m-d-Y");
echo "\n";
echo $DateEnd = date('m-d-Y', strtotime(date("m-d-Y") . ' +60 day'));
输出为::
Today is :: 11-07-2017
End date is :: 09-09-2017
如有任何帮助或建议,我们将不胜感激
问题原因:- strtotime() 注意部分:-
Dates in the m/d/y or d-m-y formats are disambiguated by looking
at the separator between the various components: if the separator is a
slash (/), then the American m/d/y is assumed; whereas if the
separator is a dash (-) or a dot (.), then the European d-m-y format
is assumed. If, however, the year is given in a two digit format and
the separator is a dash (-, the date string is parsed as y-m-d.
因此 m-d-Y
在向 php 日期添加天数时是无法识别的格式。
像下面一样:-
<?php
echo "Today is :: ".date("m-d-Y");
echo "\n";
echo $DateEnd = date('m-d-Y', strtotime(date("y-m-d") . '+60 days'));
同时检查可能的 Php date formats
阅读来自 PHP Manual 的注释:
Note:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
所以你需要使用
echo date("m-d-Y");
echo "\n";
echo $contractDateEnd = date('m-d-Y', strtotime(date("m/d/Y") . ' +60 day'));
试试这个,它会为你工作
<?php
echo "Current Date: ".date("m-d-Y");
echo "\n";
echo $contractDateEnd = date('m-d-Y', strtotime(date("d-m-Y") . ' +60 day'));
?>
您也可以使用日期间隔:
var_dump (date_create()->add(date_interval_create_from_date_string('60 days')));
您的代码有几个问题:它获取当前时间(一个数字)并将当前日期格式化为字符串然后(在将“+60 天”连接到它之后)它再次将其解析为数字只是为了再次将其格式化为字符串。很多无用又耗电的操作。真正的麻烦是 custom date format 您用于日期的 PHP 出于充分的理由不支持它。
使用DateTime
class 来处理日期和时间操作。它优于旧的 date()
、strtotime()
和其他日期和时间函数。它可以处理多个时区,代码更短更清晰:
$now = new DateTime();
$end = new DateTime();
$end->add(new DateInterval('P60D'));
echo "Today is :: " . $now->format('m-d-Y') . "\n";
echo "End date is :: " . $end->format('m-d-Y') . "\n";
我尝试在 m-d-Y 中增加天数 (60),但计算错误。它被认为是 d-m-y 格式。
echo "Today is :: ".date("m-d-Y");
echo "\n";
echo $DateEnd = date('m-d-Y', strtotime(date("m-d-Y") . ' +60 day'));
输出为::
Today is :: 11-07-2017
End date is :: 09-09-2017
如有任何帮助或建议,我们将不胜感激
问题原因:- strtotime() 注意部分:-
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.
因此 m-d-Y
在向 php 日期添加天数时是无法识别的格式。
像下面一样:-
<?php
echo "Today is :: ".date("m-d-Y");
echo "\n";
echo $DateEnd = date('m-d-Y', strtotime(date("y-m-d") . '+60 days'));
同时检查可能的 Php date formats
阅读来自 PHP Manual 的注释:
Note: Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d. To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
所以你需要使用
echo date("m-d-Y");
echo "\n";
echo $contractDateEnd = date('m-d-Y', strtotime(date("m/d/Y") . ' +60 day'));
试试这个,它会为你工作
<?php
echo "Current Date: ".date("m-d-Y");
echo "\n";
echo $contractDateEnd = date('m-d-Y', strtotime(date("d-m-Y") . ' +60 day'));
?>
您也可以使用日期间隔:
var_dump (date_create()->add(date_interval_create_from_date_string('60 days')));
您的代码有几个问题:它获取当前时间(一个数字)并将当前日期格式化为字符串然后(在将“+60 天”连接到它之后)它再次将其解析为数字只是为了再次将其格式化为字符串。很多无用又耗电的操作。真正的麻烦是 custom date format 您用于日期的 PHP 出于充分的理由不支持它。
使用DateTime
class 来处理日期和时间操作。它优于旧的 date()
、strtotime()
和其他日期和时间函数。它可以处理多个时区,代码更短更清晰:
$now = new DateTime();
$end = new DateTime();
$end->add(new DateInterval('P60D'));
echo "Today is :: " . $now->format('m-d-Y') . "\n";
echo "End date is :: " . $end->format('m-d-Y') . "\n";