Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get day difference between two date using carbon

I am using laravel and carbon package.I have two date. Now I want to get date difference from my given date.

$start_Date='2016-06-02 18:00:13';
$end_Date='2016-06-06 18:00:13';
$start = Carbon::parse($start_Date);
$end = Carbon::parse($end_Date);
$now = Carbon::now();
$length = $start->diffInDays($now);
$lengthFromEnd = $end->diffInDays($now);

if now() == 2016-06-07 then

$length will be 5 days and

$lengthFromEnd will be -1 days.

but my code return abnormal result

like image 962
Hashibul Hasan Avatar asked Dec 15 '22 05:12

Hashibul Hasan


2 Answers

you can also try this way.

$difference = $start_date->diff($end_date)->days;
like image 79
Hiren Makwana Avatar answered Dec 25 '22 11:12

Hiren Makwana


For negative result, you need a add parameter, like this:

$absolute = false;
$end->diffInDays($now, $absolute);
like image 43
Ravshanbek Ahmedov Avatar answered Dec 25 '22 09:12

Ravshanbek Ahmedov