这是一个PHP date()错误,还是有什么问题我的代码? [英] Is this a PHP date() bug, or is there something wrong with my code?

查看:121
本文介绍了这是一个PHP date()错误,还是有什么问题我的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个箭头的图像,一个向后增加一个箭头,一个通过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><?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>

问题是,当2015年2月份出现时,date()返回2015年3月-so $ month_index = 6和$ month_index = 7都是3月。



我在 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。 $月
echo。日期(F Y,$ month);

切换$ month_index = 6到$ month_index = 7仍然导致3月份被回覆。是否只有一些错误在这里,2015年的二月,...走了?



更新:谢谢大家。我从来没有发现自己。我以这种方式解决了问题:

  $ month = strtotime(date(M-01-Y)+ 。$ month_index。month); 


解决方案

这是日期如何工作,当你遇到二月和一月二十九日或以后。当您在当年2月的最后一天(即今年2月28日)之后的一个月内添加一个月时,您将跳过2月份。每隔几个月,您应该始终在本月初开始工作,以避免二月被跳过。所以,如果你从1月30日开始,并且添加一个月,因为没有2月30日你将跳过3月。



这是你可以迭代几个月,而不知道二月份有多少天(或关心)。我从现在开始选择一年的任意结束日期。

  $ 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');
}


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()错误,还是有什么问题我的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆