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

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

by therichpost
0 comment
Laravel 7.2 routing with route group auth guard check with prefix

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.

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.