Carbon: split two datetime objects by date only

Assuming I have the following code:

$now = Carbon::now();
$dateTimeObject = Carbon::parse('2017-07-20 10:16:34');

      

how do I get diff

between dates only , ignoring the time factor?

So, if it $now

is 2017-07-27 09:11:12 and the date $dateTimeObject

is 2017-07-20, there will be a difference 7

.

I need this to make sure that the results of a particular operation are only stored in the database once a day.

Note:

I tried the method diffInDays()

but it returns 0

if the values ​​are equal eg. 2016-10-12 23:56:43

and 2016-10-13 02:01:53

- so, close to midnight and at night.

+3


source to share


1 answer


Do something like this:

$now = Carbon::now()->startOfDay();
$dateTimeObject = Carbon::parse('2017-07-20 10:16:34')->startOfDay();

      



And now use diffInDays()

.

+6


source







All Articles