【问题标题】:PHP date_diff function broken?PHP date_diff 函数坏了?
【发布时间】:2017-02-19 20:03:46
【问题描述】:

考虑这两个日期 2017/4/14、2017/6/3

在 php 中使用 date_diff 给了我这个

  object(DateInterval)[6]
  public 'y' => int 0
  public 'm' => int 1
  public 'd' => int 20
  public 'h' => int 0
  public 'i' => int 0
  public 's' => int 0
  public 'weekday' => int 0
  public 'weekday_behavior' => int 0
  public 'first_last_day_of' => int 0
  public 'invert' => int 0
  public 'days' => int 50
  public 'special_type' => int 0
  public 'special_amount' => int 0
  public 'have_weekday_relative' => int 0
  public 'have_special_relative' => int 0

我的任务是创建此函数的副本(作为作业)。但是当我通过我的算法运行这些日期时,我得到了

object(stdClass)[4]
  public 'years' => int 0
  public 'months' => int 1
  public 'days' => int 19
  public 'total_days' => int 50
  public 'invert' => int 0 

当我手动(手动)计算天数时,我仍然认为这是额外的 19 天。我不是在寻找分配解决方案。但也许某些算法或 date_diff 函数有一些我不知道的错误?

对于 200 多个测试用例,我的算法有效,对于其他 7 个则无效,而且我的解决方案和 php 解决方案之间总是有天差地别。

【问题讨论】:

    标签: php datediff


    【解决方案1】:

    请记住,一个月不是固定的天数,因此这将取决于您在“1 个月”中吞食了哪个。

    如果您从 2017 年 4 月 14 日开始并先花费整整几个月,您将获得:

    • 4/14 -> 5/14:1 个月(30 天)
    • 5/14 -> 6/3:20 天

    如果你从 2017 年 6 月 3 日开始往后走,你会得到:

    • 6/3 -> 5/3:1 个月(31 天)
    • 5/3 -> 4/14:19 天

    所以这将取决于您使用的方法。

    简而言之,date_diff 在这里没有被破坏,可以说你的算法也没有被破坏 - 日期只是可以使用的时髦的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      相关资源
      最近更新 更多