错误日期期间 PHP

error date period PHP

我有这些代码,

  $end = strtotime ('last day of previous month') ;
  $start = strtotime ("-1 year +1 day", $end) ;

    $new ['first'] =  date ('Y-m-d',$start) ;
    $new ['last'] = date ('Y-m-d',$end) ;

    $interval = DateInterval::createFromDateString ('1 month') ;
    $period = new DatePeriod($start, $interval, $end) ;

    foreach ($periods as $period) {
      print_r($period -> date_format('Y-m')) ; echo "<br>";
    }

但是,我已通过此消息返回:

遇到未捕获的异常

Type: Exception

Message: DatePeriod::__construct(): This constructor accepts either (DateTimeInterface, DateInterval, int) OR (DateTimeInterface, DateInterval, DateTime) OR (string) as arguments.

谁能帮帮我?

您有语法错误和对象初始化错误。这是更正:

$end = strtotime ('last day of previous month') ;
$start = strtotime ("-1 year +1 day", $end) ;
$new['first'] =  date ('Y-m-d', $start) ;
$new['last'] = date ('Y-m-d', $end) ;

$interval = DateInterval::createFromDateString ('1 months') ;

$periods = new DatePeriod(new DateTime($new['first']), $interval, new DateTime($new['last'])) ;

foreach ($periods as $period) {
    print_r($period->format('Y-m')) ; echo "<br>";
} 

问题 1:DatePeriod 构造函数接受 DateTime 对象。请查看手册 http://php.net/manual/en/class.dateperiod.php

问题 2:您需要 $periods您在 foreach

中使用的变量

您的错误是由于试图在本应 DateTimeInterface 的地方传递 int 而导致的。

如果我们查看 DateTime class 的构造,我们会看到以下内容:

/**
 * @param DateTimeInterface $start
 * @param DateInterval $interval
 * @param DateTimeInterface $end
 * @param int $options Can be set to DatePeriod::EXCLUDE_START_DATE.
 * @link http://php.net/manual/en/dateperiod.construct.php
 * @since 5.3.0
 */
public function __construct (DateTimeInterface $start, DateInterval $interval, DateTimeInterface $end, $options=0) {}

这可以通过转换为正确的数据类型轻松解决:

$end = strtotime ('last day of previous month') ;
$start = strtotime ("-1 year", $end) ;

$new ['first'] =  date ('Y-m-d',$start) ;
$new ['last'] = date ('Y-m-d',$end) ;

$interval = DateInterval::createFromDateString ('1 month') ;
$period = new DatePeriod((new DateTime())->setTimestamp($start), $interval, (new DateTime())->setTimestamp($end)) ;

这将成功满足 DatePeriod class 中存在的类型提示。


这是相关部分:

(new DateTime())->setTimestamp($start)

您还可以查看手册中的DatePeriod class。

问题出在这里$period = new DatePeriod($start, $interval, $end) ;

DatePeriod 的构造函数是:

public DatePeriod::__construct ( DateTimeInterface $start , DateInterval $interval , int $recurrences [, int $options ] )

即。它需要 DateTimeInterface 类型的对象,而您正在将日期对象传递给它。您还创建了一个名为 $period 的 DatePeriod 对象,并尝试在 foreach 循环中使用 $periods ..(似乎是错字) 即使您在这里有答案...这是我尝试并为我工作的东西。

<?php
        $end = strtotime ('last day of previous month') ;
        $start = strtotime ("-1 year +1 day", $end) ;

        $new ['first'] =  date ('Y-m-d',$start) ;
        $new ['last'] = date ('Y-m-d',$end) ;

        $interval = DateInterval::createFromDateString ('1 month') ;
        $period = new DatePeriod(new DateTime(date('Y-m-d',$start)), $interval, $end) ;

        foreach ($period as $pd) {
            print_r(date_format($pd,'Y-m')); echo "<br/>";
        }
    ?>