在PHP中无法从DateTime获取上个月这是一个(相当大的)bug吗? [英] Can't get previous month from DateTime in PHP- Is this a (pretty big) bug?

查看:122
本文介绍了在PHP中无法从DateTime获取上个月这是一个(相当大的)bug吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在PHP中创建函数,让我加强/减少给定的日期时间单位。具体来说,我需要能够从当前的月份转到下一个/上个月。

I need to create functions in PHP that let me step up/down given datetime units. Specifically, I need to be able to move to the next/previous month from the current one.

我以为可以使用DateTime :: add / sub(P1M )。然而,当试图获得上一个月时,如果日期值= 31-看起来实际上是要计算30天,而不是减少月份值,它就会混乱!:

I thought I could do this using DateTime::add/sub(P1M). However, when trying to get the previous month, it messes up if the date value = 31- looks like it's actually trying to count back 30 days instead of decrementing the month value!:

$prevMonth = new DateTime('2010-12-31'); 

尝试减少月份:

$prevMonth->sub(new DateInterval('P1M')); // = '2010-12-01'
$prevMonth->add(DateInterval::createFromDateString('-1 month')); // = '2010-12-01'
$prevMonth->sub(DateInterval::createFromDateString('+1 month')); // = '2010-12-01'
$prevMonth->add(DateInterval::createFromDateString('previous month')); // = '2010-12-01'

这当然似乎是错误的行为。任何人都有洞察力
谢谢 -

This certainly seems like the wrong behavior. Anyone have any insight? Thanks-

注意: PHP版本5.3.3

NOTE: PHP version 5.3.3

推荐答案

(信用实际上属于Alex,在评论中指出这一点)

(Credit actually belongs to Alex for pointing this out in the comments)

问题不是PHP,而是GNU一,如下所述:

The problem is not a PHP one but a GNU one, as outlined here:

日期字符串中的相对项目

这里的关键是区分这个日期的最后一个月,因为月份是不同数量日期的模糊单位,不可能定义为12月31日(因为11月31日不存在),上个月,不论日期的概念,

The key here is differentiating between the concept of 'this date last month', which, because months are 'fuzzy units' with different numbers of dates, is impossible to define for a date like Dec 31 (because Nov 31 doesn't exist), and the concept of 'last month, irrespective of date'.

如果我们感兴趣的是上一个月,那么gaurantee适当的DateInterval计算的唯一方法是将日期值重置为1st或其他每个月都会有的数字。

If all we're interested in is the previous month, the only way to gaurantee a proper DateInterval calculation is to reset the date value to the 1st, or some other number that every month will have.

真正触及我的是在PHP和其他地方,这个问题是如何无关的,考虑到可能影响到多少依赖日期的软件。

What really strikes me is how undocumented this issue is, in PHP and elsewhere- considering how much date-dependent software it's probably affecting.

这是一个安全的处理方法:

Here's a safe way to handle it:

/*
Handles month/year increment calculations in a safe way,
avoiding the pitfall of 'fuzzy' month units.

Returns a DateTime object with incremented month/year values, and a date value == 1.
*/
function incrementDate($startDate, $monthIncrement = 0, $yearIncrement = 0) {

    $startingTimeStamp = $startDate->getTimestamp();
    // Get the month value of the given date:
    $monthString = date('Y-m', $startingTimeStamp);
    // Create a date string corresponding to the 1st of the give month,
    // making it safe for monthly/yearly calculations:
    $safeDateString = "first day of $monthString";
    // Increment date by given month/year increments:
    $incrementedDateString = "$safeDateString $monthIncrement month $yearIncrement year";
    $newTimeStamp = strtotime($incrementedDateString);
    $newDate = DateTime::createFromFormat('U', $newTimeStamp);
    return $newDate;
}

这篇关于在PHP中无法从DateTime获取上个月这是一个(相当大的)bug吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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