dongtazu3080 2016-10-31 09:03 采纳率: 100%
浏览 64
已采纳

错误日期PHP

i have these code,

  $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>";
    }

but, I have returned by this message:

An uncaught Exception was encountered

Type: Exception

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

anyone can help me?

  • 写回答

3条回答 默认 最新

  • duanliaoyin3171 2016-10-31 09:13
    关注

    You have syntax error and object initialization error. Here is the correction:

    $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>";
    } 
    

    Problem 1: DatePeriod constructor accepts DateTime object. Please check manual http://php.net/manual/en/class.dateperiod.php

    Problem 2: you need $periodsvariable as you used in foreach

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据