添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account "diffInDays(null)" behaves differently when using "new Carbon('00-00-0000')" than "Carbon::create(0000, 00, 00)" "diffInDays(null)" behaves differently when using "new Carbon('00-00-0000')" than "Carbon::create(0000, 00, 00)" MohsenElgendy opened this issue Jan 21, 2017 · 5 comments

As the documentation states under the Difference section:

Each function below has a default first parameter which is the Carbon instance to compare to, or null if you want to use now()

When I tried to get the difference in days between a due date and "now", The output was not the same, Take a look at the following code

assuming today is "21-01-2017"

( new Carbon('15-02-2017') )->diffInDays(null); // returns 24. Invalid
Carbon::create(2017, 02, 15)->diffInDays(null); // returns 25. Valid

If today is "21-01-2017" and due date "15-02-2017"; the difference should be "25" days.

That only happens when setting the first parameter of diffInDays() to null. But instead; If we used a new Carbon instance with the date of "today" hard-written, it works fine.

Consider the following example:
( new Carbon('15-02-2017') )->diffInDays(new Carbon('21-01-2017')); // returns 25. Valid
Carbon::create(2017, 02, 15)->diffInDays(new Carbon('21-01-2017')); // returns 25. Valid

Is this considered an issue ? Or is there something I am missing ?

@lucasmichot Sorry for not following up.

Here is the result of var_dump 'ing both instances. Turns out there is a difference in the date variable.

var_dump( new Carbon('15-02-2017') )
object(Carbon\Carbon)#268 (3) {
  ["date"]=>
  string(26) "2017-02-15 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(12) "Africa/Cairo"
var_dump( Carbon::create(2017, 02, 15) )
object(Carbon\Carbon)#269 (3) {
  ["date"]=>
  string(26) "2017-02-15 17:07:24.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(12) "Africa/Cairo"

17:07:24.000000 was the same time I ran the dump script.

It is still not clear to me,
Would this require a fix or there is something I should be aware of ?

When you create a new instance like new Carbon('15-02-2017') it will set the time to 00:00:00 for the determined timezone when no specific time is passed.
When using Carbon::create(2017, 02, 15) it will set the time to the current time of day for the determined timezone when no specific time is passed.
It is documented here for the create method, so its result is expected.
I guess the only question is whether it should be expected that omitting a time when passing a string to the constructor will result in the time defaulting to 00:00:00.

In regards to the original issue with using the difference method. Since Carbon represents both the date and the time, not just the date, if I need to calculate anything based on whole days I will always reset each value to the beginning of the day to remove any time discrepancies like so:

$dateA = Carbon::create(2017, 02, 15)->startOfDay();
$dateB = Carbon::now()->startOfDay();
$dateA->diffInDays($dateB);