Categories

Friday, December 27, 2024
#919814419350 therichposts@gmail.com
Laravel

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

Laravel 7.2 routing with route group auth guard check with prefix

get date difference in hours and days with laravel carbon

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.

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

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