• <i id='BvFVS'><tr id='BvFVS'><dt id='BvFVS'><q id='BvFVS'><span id='BvFVS'><b id='BvFVS'><form id='BvFVS'><ins id='BvFVS'></ins><ul id='BvFVS'></ul><sub id='BvFVS'></sub></form><legend id='BvFVS'></legend><bdo id='BvFVS'><pre id='BvFVS'><center id='BvFVS'></center></pre></bdo></b><th id='BvFVS'></th></span></q></dt></tr></i><div id='BvFVS'><tfoot id='BvFVS'></tfoot><dl id='BvFVS'><fieldset id='BvFVS'></fieldset></dl></div>
    • <bdo id='BvFVS'></bdo><ul id='BvFVS'></ul>

      <tfoot id='BvFVS'></tfoot>

      <small id='BvFVS'></small><noframes id='BvFVS'>

        <legend id='BvFVS'><style id='BvFVS'><dir id='BvFVS'><q id='BvFVS'></q></dir></style></legend>

        这是一个 PHP date() 错误,还是我的代码有问题?

        时间:2023-10-12

          <small id='382pr'></small><noframes id='382pr'>

        • <legend id='382pr'><style id='382pr'><dir id='382pr'><q id='382pr'></q></dir></style></legend>

            <i id='382pr'><tr id='382pr'><dt id='382pr'><q id='382pr'><span id='382pr'><b id='382pr'><form id='382pr'><ins id='382pr'></ins><ul id='382pr'></ul><sub id='382pr'></sub></form><legend id='382pr'></legend><bdo id='382pr'><pre id='382pr'><center id='382pr'></center></pre></bdo></b><th id='382pr'></th></span></q></dt></tr></i><div id='382pr'><tfoot id='382pr'></tfoot><dl id='382pr'><fieldset id='382pr'></fieldset></dl></div>

                  <tbody id='382pr'></tbody>
                • <bdo id='382pr'></bdo><ul id='382pr'></ul>
                  <tfoot id='382pr'></tfoot>
                • 本文介绍了这是一个 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() 错误,还是我的代码有问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:有没有办法让 PhpStorm 的自动完成“更深入"? 下一篇:如果未选择日期,如何防止 bootstrap-datepicker 从输入中清除现有值?

                  相关文章

                  <i id='HnDk7'><tr id='HnDk7'><dt id='HnDk7'><q id='HnDk7'><span id='HnDk7'><b id='HnDk7'><form id='HnDk7'><ins id='HnDk7'></ins><ul id='HnDk7'></ul><sub id='HnDk7'></sub></form><legend id='HnDk7'></legend><bdo id='HnDk7'><pre id='HnDk7'><center id='HnDk7'></center></pre></bdo></b><th id='HnDk7'></th></span></q></dt></tr></i><div id='HnDk7'><tfoot id='HnDk7'></tfoot><dl id='HnDk7'><fieldset id='HnDk7'></fieldset></dl></div>

                  <legend id='HnDk7'><style id='HnDk7'><dir id='HnDk7'><q id='HnDk7'></q></dir></style></legend>

                    1. <small id='HnDk7'></small><noframes id='HnDk7'>

                      • <bdo id='HnDk7'></bdo><ul id='HnDk7'></ul>

                      <tfoot id='HnDk7'></tfoot>