• <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() 错误,还是我的代码有问题?

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

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  <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>
                          <tbody id='HnDk7'></tbody>

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