PHP DateInterval不返回本月最后一天 [英] PHP DateInterval not returning last day of the month

查看:140
本文介绍了PHP DateInterval不返回本月最后一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码:

  public static function getDays($ day,$ m,$ y){ 
$ days = new DatePeriod(
new DateTime(first $ day of $ m $ y),
DateInterval :: createFromDateString('next'。$ day),
新的DateTime(最后一天$ m $ y)
);
foreach($ days as $ day){
$ dates [] = $ day->格式(Y-m-d);
}
return $ dates;
}

它的工作大部分是。我给它一个范围,它返回我在范围之内的每一天的日期。



用法:

  foreach(General :: getDays('friday',$ this-> _uri [2],$ this-> _uri [3])as $ date){
var_dump($ date);
}

2013年7月,这将返回正确的4个结果,所有星期五在2013年7月:

  string'2013-07-05'(length = 10)
string'2013-07-12' (长度= 10)
string'2013-07-19'(length = 10)
string'2013-07-26'(length = 10)
但是,在一个月的最后一天是与我之后发生的一天(例如星期五,2013年5月和2014年1月),它错过了结果返回结果的最后一天。



2013年5月的结果,当然是31日星期五缺席:

  string'2013-05-03'(length = 10)
string'2013-05-10'(length = 10)
string '2013-05-17'(length = 10)
string'2013-05-24'(length = 10)

有没有人有这个答案?这是我的错误使用DateInterval还是这个类的真正错误?

解决方案

问题是结束日期/时间本周期从未包含在此期间 - 即 DatePeriod 对象表示中的日期/时间[$ startDate,$ endDate] range¹。



您必须至少在结束日期添加一秒钟才能始终获得预期的结果。但是让我们不要吝啬并添加一整分钟:

  $ days = new DatePeriod(
new DateTime

new DateTime(最后一天$ m $ y 00:01)$ b $($ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $)
DateInterval :: createFromDateString b);






¹除非您使用 DatePeriod :: EXCLUDE_START_DATE 选项,在这种情况下,开始日期本身也被排除在外


I'm using the following code:

public static function getDays($day, $m, $y) {
  $days = new DatePeriod(
    new DateTime("first $day of $m $y"),
    DateInterval::createFromDateString('next ' . $day),
    new DateTime("last day of $m $y")
  );
  foreach ($days as $day) {
    $dates[] = $day->format("Y-m-d");
  }
  return $dates;
}

It work's for the most part. I give it a range and it returns the date of each day I am after inside the range.

Usage:

foreach(General::getDays('friday',$this->_uri[2],$this->_uri[3]) as $date) {
  var_dump($date);
}

In July 2013, this returns the correct 4 results, all friday's in July 2013:

string '2013-07-05' (length=10)
string '2013-07-12' (length=10)
string '2013-07-19' (length=10)
string '2013-07-26' (length=10)

However, on a month that the last day is a match to the day occurance I am after (friday for example, May 2013 and January 2014) it misses the last day of the month out of the returned results.

May 2013 results, missing of course the 31st which is a friday:

string '2013-05-03' (length=10)
string '2013-05-10' (length=10)
string '2013-05-17' (length=10)
string '2013-05-24' (length=10)

Does anyone have an answer for this? Is this my miss-use of DateInterval or a genuine fault in this class?

解决方案

The problem is that the end date/time itself is never included in the period -- that is, the DatePeriod object represents date/times in the [$startDate, $endDate) range¹.

You have to add one second at least to the end date to always get the expected results. But let's not be stingy and add a whole minute:

$days = new DatePeriod(
    new DateTime("first $day of $m $y"),
    DateInterval::createFromDateString('next ' . $day),
    new DateTime("last day of $m $y 00:01")
);


¹ unless you use the DatePeriod::EXCLUDE_START_DATE option, in which case the start date itself is excluded as well

这篇关于PHP DateInterval不返回本月最后一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆