【问题标题】:Array of date years and months日期年月数组
【发布时间】:2018-06-13 12:22:32
【问题描述】:

我在网上搜索并发现了类似的问题,但不完全是我所追求的。我基本上是在将 2 个数组传递给 API 之后,因此它们需要采用正确的格式。我传的第一个数组是这样的

$dateArray = array(
    '2015' => '2015-01-01T00:00:00',
    '2016' => '2016-01-01T00:00:00',
    '2017' => '2017-01-01T00:00:00',
    '2018' => '2018-01-01T00:00:00'
);

目前这是硬编码的,但我想让它动态化,所以我不必更新它。我所知道的是,开始年份是 2015 年,结束年份将永远是当前年份。目前我正在尝试这个

$years = array_combine(range(date("Y"), 2015), range(date("Y"), 2015));

这给了我类似以下的东西

array:4 [
  2018 => 2018
  2017 => 2017
  2016 => 2016
  2015 => 2015
]

现在这几乎是正确的,年份的顺序无关紧要,但我不确定如何最好地将这部分 2015-01-01T00:00:00 放在数组的右侧?

我追求的第二个数组非常相似。开始始终是 01/2015,当前始终是当前月份和年份。硬编码的版本如下所示

$dateArray = array(
    '2015-01' => '2015-01-01T00:00:00',
    '2015-02' => '2015-02-01T00:00:00',
    '2015-03' => '2015-03-01T00:00:00',
    '2015-04' => '2015-04-01T00:00:00',
    ...
);

这个我不确定,我试过了

$years = array_combine(range(date("Y-m"), 2015-01), range(date("Y-m"), 2015-01));

但这似乎只输出年份。如何动态生成这个数组?

【问题讨论】:

    标签: php date date-range


    【解决方案1】:

    如果它总是从 2015 年开始,并且总是从 1 月 1 日开始,如图所示,那么一个简单的循环应该可以工作。

    for ($Y = 2015; $Y <= date('Y'); $Y++) {
        $yearArray[$Y] = $Y . '-01-01T00:00:00';
        $months = $Y < date('Y') ? 12 : date('n');
    
        for ($m = 1; $m <= $months; $m++) {
            $arrayKey = sprintf('%d-%02d', $Y, $m);
            $monthArray[$arrayKey] = $arrayKey . '-01T00:00:00';
        }
    }
    

    【讨论】:

      【解决方案2】:

      我建议您使用 DateTime::createFromFormat() 方法使用 DateTime 类。

      <?php
      
      $startYear = 2010;
      $array = [];
      
      for ($x = $startYear; $x <= date('Y'); $x ++) {
          $date = DateTime::createFromFormat('Y-m-d\TH:i:s', $x . '-01-01T00:00:00');
          $array[$x] = $date->format('Y-m-d\TH:i:s');
      }
      
      var_dump($array);
      

      这将输出:

      array(9) { 
      [2010]=> string(19) "2010-01-01T00:00:00" 
      [2011]=> string(19) "2011-01-01T00:00:00" 
      [2012]=> string(19) "2012-01-01T00:00:00" 
      [2013]=> string(19) "2013-01-01T00:00:00" 
      [2014]=> string(19) "2014-01-01T00:00:00" 
      [2015]=> string(19) "2015-01-01T00:00:00" 
      [2016]=> string(19) "2016-01-01T00:00:00" 
      [2017]=> string(19) "2017-01-01T00:00:00" 
      [2018]=> string(19) "2018-01-01T00:00:00" 
      }
      

      点击这里https://3v4l.org/9gVRg可以自己查看

      理论上你可以只用字符串来做,但是很值得你花时间玩DateTime类。 http://php.net/manual/en/class.datetime.php,甚至可能只是在数组中存储对象而不是字符串。

      这是获取第二个数组的方法,我以稍微不同的方式完成了该操作,只是为了向您展示另一种可以完成的方式。

      <?php
      
      $array2 = [];
      $year = 2010;
      for ($x = 1; $x <= 12; $x ++) {
          $array2[$year.'-'.$x] = (new DateTime($year.'-'.$x.'-01T00:00:00'))->format('Y-m-d\TH:i:s');
      }
      
      var_dump($array2);
      

      再一次,这里的代码https://3v4l.org/C5hQ7

      【讨论】:

      • “我建议你使用 DateTime 类” - 为什么?这里任何地方都不需要实际的日期“算术”......所以像其他答案一样简单地附加静态前缀,似乎更直接......
      【解决方案3】:

      如果您出于某种原因不喜欢代码中的循环,可以在 $years 上运行它。

      array_walk($years, function(&amp;$val) { $val = sprintf('%d-01-01T00:00:00', $val); });

      【讨论】:

        【解决方案4】:
        <?php
        $keyArr = range(date("Y"), 2015);
        $valArr = array_map(function($a){return $a.'-01-01-T00:00:00';},$keyArr);
        $years = array_combine($keyArr,$valArr);
        ?>
        

        这将为您提供所需的输出。

        【讨论】:

          【解决方案5】:

          @MonkeyZeus 的略微修改版本,使用 DateInterval 类:

          $start = new DateTime( '2015-01-01' );
          // without `noon` it won't work if now were the first of a month
          $end = new DateTime( 'today noon' ); 
          $interval = new DateInterval( 'P1M' );
          
          $period = new DatePeriod( $start, $interval, $end );
          
          $data = array();
          foreach ( $period as $date ) {
              // it would make more sense to use DATE_W3C as format ...
              $data[ $date->format( 'Y-m' ) ] = $date->format( 'Y-m-d\TH:i:s' );
          }
          

          【讨论】:

          • 如果使用了$start = new DateTime( '2015-01-30' );,则无法捕获 2015 年 2 月。
          【解决方案6】:

          应该这样做:

          <?php
          // Store your years here
          $years = array();
          
          // Store the monthly dates here
          $months = array();
          
          // This is the starting point
          $start = new DateTime( '2015-01-01 00:00:00' );
          
          // while we are working with a date which is less than the current time then keep building the array
          while( $start->getTimeStamp() <= time() )
          {
              // Populate a year
              $years[ $start->format( 'Y' ) ] = $start->format( 'Y' ).'-01-01T00:00:00';
          
              // Populate a month
              $months[ $start->format( 'Y-m' ) ] = $start->format( 'Y-m' ).'-01T00:00:00';
          
              // Always add a month since that is the smallest interval we need to store
              $start->modify( 'first day of +1 month' );
          }
          
          print_r( $years );
          print_r( $months );
          

          输出:

          Array
          (
              [2015] => 2015-01-01T00:00:00
              [2016] => 2016-01-01T00:00:00
              [2017] => 2017-01-01T00:00:00
              [2018] => 2018-01-01T00:00:00
          )
          Array
          (
              [2015-01] => 2015-01-01T00:00:00
              [2015-02] => 2015-02-01T00:00:00
              [2015-03] => 2015-03-01T00:00:00
              [2015-04] => 2015-04-01T00:00:00
              [2015-05] => 2015-05-01T00:00:00
              [2015-06] => 2015-06-01T00:00:00
              [2015-07] => 2015-07-01T00:00:00
              [2015-08] => 2015-08-01T00:00:00
              [2015-09] => 2015-09-01T00:00:00
              [2015-10] => 2015-10-01T00:00:00
              [2015-11] => 2015-11-01T00:00:00
              [2015-12] => 2015-12-01T00:00:00
              [2016-01] => 2016-01-01T00:00:00
              [2016-02] => 2016-02-01T00:00:00
              [2016-03] => 2016-03-01T00:00:00
              [2016-04] => 2016-04-01T00:00:00
              [2016-05] => 2016-05-01T00:00:00
              [2016-06] => 2016-06-01T00:00:00
              [2016-07] => 2016-07-01T00:00:00
              [2016-08] => 2016-08-01T00:00:00
              [2016-09] => 2016-09-01T00:00:00
              [2016-10] => 2016-10-01T00:00:00
              [2016-11] => 2016-11-01T00:00:00
              [2016-12] => 2016-12-01T00:00:00
              [2017-01] => 2017-01-01T00:00:00
              [2017-02] => 2017-02-01T00:00:00
              [2017-03] => 2017-03-01T00:00:00
              [2017-04] => 2017-04-01T00:00:00
              [2017-05] => 2017-05-01T00:00:00
              [2017-06] => 2017-06-01T00:00:00
              [2017-07] => 2017-07-01T00:00:00
              [2017-08] => 2017-08-01T00:00:00
              [2017-09] => 2017-09-01T00:00:00
              [2017-10] => 2017-10-01T00:00:00
              [2017-11] => 2017-11-01T00:00:00
              [2017-12] => 2017-12-01T00:00:00
              [2018-01] => 2018-01-01T00:00:00
              [2018-02] => 2018-02-01T00:00:00
              [2018-03] => 2018-03-01T00:00:00
              [2018-04] => 2018-04-01T00:00:00
              [2018-05] => 2018-05-01T00:00:00
              [2018-06] => 2018-06-01T00:00:00
          )
          

          【讨论】:

            猜你喜欢
            • 2012-02-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-02-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多