PHP date_diff函数损坏? [英] PHP date_diff function broken?

查看:84
本文介绍了PHP date_diff函数损坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Consider these two dates 2017/4/14, 2017/6/3

在php中使用date_diff可以给我这个

Using date_diff in php gives me this

  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

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

I have a task of creating a replicate of this function (as a assignment). But when I run these dates through my algorithm I get

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函数有一些我不知道的错误?

And when I calculate the number of days by hand (manually) I still see this as 19 extra days. I am not looking for the assignment solution. But maybe some algorithm or maybe the date_diff function has some bugs I am not aware of?

对于200多个测试用例,我的算法有效,而对于其他7个案例,它却不起作用,

For 200+ test cases my algorithm works, for 7 others it does not and it's always a day difference between my solution and php solution.

推荐答案

请记住,一个月不是固定的天数,因此,这取决于您在 1个月内所处的位置。

Keep in mind that a month is not a fixed number of days, so it's going to depend which you gobble up into that "1 month."

如果您从2017年4月14日开始,并且先花整整几个月的时间,则会得到:

If you start from 2017/4/14 and take full months first you'll get:


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

  • 5 / 14-> 6/3:20天

如果您从2017/6/3开始并往回走,得到:

While if you start from 2017/6/3 and work your way backward you'll get:


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

  • 5/3-> 4/14:19天

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

So it's going to depend on the method you're using.

简而言之, date_diff 在这里没有坏掉,而且可以说您的算法也不是-日期只是放克y可以使用的东西。

In short date_diff isn't broken here, and arguably your algorithm isn't either - dates are just funky things to work with.

这篇关于PHP date_diff函数损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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