Laravel

How to get date difference in hours and days with laravel carbon?

get date difference in hours and days with laravel carbon

Views

Hello to all, welcome to therichpost.com. In this post, I will tell you, How to get date difference in hours and days with laravel carbon? Like I always say that laravel is the best Mvc php framework.

Carbon class is inherited from the PHP DateTime class.

Here is working and tested code and you can add this code in laravel controller file:
public function custom_date_diff($previous_date){

$fdate = "2018-06-03";
$tdate = Carbon::now()->toDateTimeString();
$datetime1 = new \DateTime($fdate);
$datetime2 = new \DateTime($tdate);
$interval = $datetime1->diff($datetime2);
$hours = $interval->h;//now do whatever you like with $hours
$days = $interval->format('%a');//now do whatever you like with $days
return $hours;

}

 If you have any query related to this post then please do comment below and I will come with more laravel posts.

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

Positive SSL