dsf12313 2016-01-15 22:19
浏览 27
已采纳

PHP的DateTime数组似乎有些偏差

I am trying to create a function to create an array of DateTime(s). The dates I want are every Saturday in a given year.

I have a function that does this but when the values are stored in an array, they begin with the second Saturday of the year and extend to the first Saturday of the following year.

Note that the following displays a list of Saturdays as generated by the function and then after a couple of blank lines displays the Saturdays store in the array.

<?php
define ('sql','Y-m-d');
define ('br','<br/>');

function allSaturdays ($year){
    $endofyear = "$year-12-31";
    $interval = new DateInterval("P7D");

    $year--;
    $workingdate = "$year-12-31";  

    $workingdate  = strtotime ($workingdate);
    $workingdate  = strtotime ("next Saturday",$workingdate); 

    $workingdate  = date ("Y-m-d",$workingdate);   
    $workingdate  = new DateTime ($workingdate); 
    $result[] = new DateTime;
    while ($workingdate->format(sql) <= $endofyear ) {
       $result[] = $workingdate;
       echo $workingdate->format (sql).br;     
       $workingdate->add ($interval);
       $workingdate = new DateTime ($workingdate->format(sql));
       //echo $workingdate->format (sql)."#".br;;
    }// while
      unset ($workingdate);
      return $result;  

 }//function


  $sats = allSaturdays(2016);

  echo "<br/.<br/>";
  foreach ($sats as $saturday)
     echo $saturday->format(sql)."*<br>";


  ?>

However, if I store the only the date (2016-01-02) the correct values are in the array.

No doubt I'm missing something simple. Any help is appreciated.

Dave

  • 写回答

1条回答 默认 最新

  • douxueke5653 2016-01-15 22:23
    关注

    You are modifying your DateTime object after you placed it in the array.

    This is where you add it to the array:

    $result[] = $workingdate;
    

    And then the line after that adds 7 days to it:

    $workingdate->add($interval);
    

    Calling add modifies the original. If you want to make sure you're working on a copy, call clone first or use DateTimeImmutable instead of DateTime.

    Here's a new version of your code using DateTimeImmutable:

    function allSaturdays ($year){
    
        $endOfYear = new DateTimeImmutable("$year-12-31");
        $workingdate = new DateTimeImmutable("first saturday of January " . $year);
    
        while ($workingdate <= $endofyear ) {
           $result[] = $workingdate;
           $workingDate = $workingDate->modify('+7 days');
        }// while
        return $result;  
    
     }//function
    

    Here's another version that does not use DateTimeImmutable

    function allSaturdays ($year){
    
        $endOfYear = new DateTime("$year-12-31");
        $workingdate = new DateTime("first saturday of January " . $year);
    
        while ($workingdate <= $endofyear ) {
           $result[] = $workingdate;
           $workingDate = clone $workingDate;
           $workingDate->modify('+7 days');
        }// while
        return $result;  
    
     }//function
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)