本文介绍了这是一个 PHP date() 错误,还是我的代码有问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两张箭头图像,一张向后增加月份,另一张通过 href 向前增加.
if (ISSET($_GET["month"])){$month_index = $_GET["month"];}别的{$month_index = 0;}$month = strtotime("+".$month_index."month");?>...<a href=<?php $month_index = $month_index - 1;echo "?month=".$month_index;?>><img src="arrow_left.gif" ></a><div class="title">Logbook Calendar for <?php echo date("F Y",$month);?>
<a href=<?php $month_index = $month_index + 2;echo "?month=".$month_index;?>><img src="arrow_right.gif"></a>
问题是当 2015 年 2 月到来时,date() 返回2015 年三月"——所以 $month_index = 6 和 $month_index = 7 都是三月.
我在 http://writecodeonline.com/php/ 上运行此代码:
date_default_timezone_set("America/New_York");$month_index = 6;$month_index = $month_index - 1;$month_index = $month_index + 2;回声 $month_index;$month = strtotime("+".$month_index."month");回声".$月;回声".日期(F Y",$月);
将 $month_index=6 切换为 $month_index=7 仍然会导致 3 月被回显.2015 年 2 月已经过去的地方是不是有一些错误......消失了?
更新:谢谢大家.我永远不会发现我自己.我是这样解决问题的:
$month = strtotime(date("M-01-Y") . "+".$month_index."month");
解决方案
这是日期的工作方式以及当您遇到 2 月和该月的第 29 天或之后的日期.当您在该年 2 月的最后一天(即今年 2 月 28 日)之后的某个日期添加一个月时,您将跳过 2 月.每当迭代几个月时,您应该始终在月初工作,以避免跳过二月.因此,如果您从 1 月 30 日开始并添加一个月",因为没有 2 月 30 日,您将跳到 3 月.
以下是您在不知道二月(或关心)有多少天的情况下迭代几个月的方法.我选择了从现在起一年后的任意结束日期.
$start = new DateTimeImmutable('@'.mktime(0, 0, 0, $month_index, 1, 2014));$end = $start->modify('+1 年')$interval = new DateInterval('P1M');$period = new DatePeriod($start, $interval, $end);foreach ($period as $dt) {echo $dt->format('F Y');}
I've two images of an arrow, one which increments the month backward, one which increments it forward via href.
if (ISSET($_GET["month"])){
$month_index = $_GET["month"];
}
else{
$month_index = 0;
}
$month = strtotime("+".$month_index." month");
?>
...
<a href=<?php $month_index = $month_index - 1; echo "?month=".$month_index; ?>><img src="arrow_left.gif" ></a>
<div class="title">Logbook Calendar for <?php echo date("F Y",$month); ?> </div>
<a href=<?php $month_index = $month_index + 2; echo "?month=".$month_index; ?>><img src="arrow_right.gif"></a>
The issue is that when Feburary 2015 comes up, date() returns "March 2015"—so $month_index = 6 and $month_index = 7 are both March.
I ran this code on http://writecodeonline.com/php/:
date_default_timezone_set("America/New_York");
$month_index = 6;
$month_index = $month_index - 1;
$month_index = $month_index + 2;
echo $month_index;
$month = strtotime("+".$month_index." month");
echo " " . $month;
echo " " . date("F Y",$month);
Switching $month_index=6 to $month_index=7 still results in March being echoed. Is there just some bug here where Feburary, 2015 is... gone?
Update: Thanks, everyone. I would've never found that on my own. I solved the problem this way:
$month = strtotime(date("M-01-Y") . "+".$month_index." month");
解决方案
It's how dates work and when you encounter February and the 29th day of the month or later. When you add a month to a date after the last day of February of that year (i.e. 28th of February this year) you will skip over February. Whenever iterating through months you should always work with the start of the month to avoid February being skipped. So if you start with January 30th and add "one month" since there is no Feb 30th you will skip ahead to March.
Here's how you can iterate through months without knowing how many days are in February (or caring). I picked an arbitrary end date of one year from now.
$start = new DateTimeImmutable('@'.mktime(0, 0, 0, $month_index, 1, 2014));
$end = $start->modify('+1 year')
$interval = new DateInterval('P1M');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format('F Y');
}
这篇关于这是一个 PHP date() 错误,还是我的代码有问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!