Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vandyczech's avatar

Carbon wrong date difference calculations

I have two dates

// get date from the database column created_at 

$dt = \Carbon\Carbon::parse( $message->created_at );

// get today

$today = \Carbon\Carbon::today();

// counting difference

if( \Carbon\Carbon::parse( $message->created_at )->diffInDays( \Carbon\Carbon::today()) == 7 ){

   return 'one week';

}

Bad when the $dt = 22.2.2018 and $today is 2.3.2018, Carbon count 7 days and return one week but, difference in real is 8 days. How compare two dates only via dates, without timestamp but create from created_at what is the timestamp ... ?

0 likes
11 replies
jlrdw's avatar

Verify time zone is correct.

vandyczech's avatar

Timezone is right Europe/Prague ... but still wrong ..

jlrdw's avatar

Formats have to match in the calculation.

1 like
jlrdw's avatar

Bad when the $dt = 22.2.2018 and $today is 2.3.2018 are not the same formats.

Snapey's avatar
Snapey
Best Answer
Level 122

difference is 7 days and xx hours not 8 days, and it probably depends what time of day your created_at is.

By the way, created_at is already an instance of Carbon.

In Tinker;

>>> $dt=Carbon\Carbon::parse('2018-02-22 00:00:00')
=> Carbon\Carbon @1519257600 {#821
     date: 2018-02-22 00:00:00.0 UTC (+00:00),
   }
>>> $dt->diffInDays()
=> 8
>>> $dt=Carbon\Carbon::parse('2018-02-22 23:00:00')
=> Carbon\Carbon @1519340400 {#822
     date: 2018-02-22 23:00:00.0 UTC (+00:00),
   }
>>> $dt->diffInDays()
=> 7
>>>

its 20:40 when I ran this

You can clear the time from a carbon timestamp with StartOfDay

>>> $dt=Carbon\Carbon::parse('2018-02-22 23:00:00')
=> Carbon\Carbon @1519340400 {#809
     date: 2018-02-22 23:00:00.0 UTC (+00:00),
   }
>>> $dt->startOfDay()->diffInDays()
=> 8

By the way, if you don't provide a timestamp to diffInDays then it assumes now()

2 likes
vandyczech's avatar

@Snapey In real is the difference is 8 days, carbon count only 8. I compare created_at instance of Carbon and Carbon today, it is the same format right?

Snapey's avatar
if( $message->created_at->startOfDay->diffInDays() == 7 ){

   return 'one week';

}

1 like
NoLAstNamE's avatar

@Snapey just in case someone is looking into this, here's the updated one.

if( $message->created_at->startOfDay()->diffInDays() == 7 ){
   return 'one week';
}

// before
->startOfDay->

// after
->startOfDay()->
Snapey's avatar

In my example, if the created_at time is 23:00 and it is only 20:00 now then the difference is 7 days and 21 hours - NOT 8 days.

>>> $dt=Carbon\Carbon::parse('2018-02-22 23:00:00')
=> Carbon\Carbon @1519340400 {#813
     date: 2018-02-22 23:00:00.0 UTC (+00:00),
   }
>>> $dt->diffForHumans(Carbon\Carbon::now(),false,false,2)
=> "1 week 21 hours before"
>>>

This, multi-part answer for diffForHumans is in version 1.23.0 of Carbon, released 3 days ago

vandyczech's avatar

startOfDay() is best solution ... and works fine

Please or to participate in this conversation.