Welcome To ITSolutionsGuides,
In Laravel, Carbon is a powerful date and time manipulation library that comes pre-installed with the framework. One of its useful methods is diffForHumans, which allows you to display time differences in a human-readable format. Here's how you can use diffForHumans effectively in your Laravel application. You can use the diffForHumans method on a Carbon instance to display the difference between two dates or times in a human-readable format. This method returns a string representing the difference in a descriptive manner such as "2 hours ago", "3 days from now", etc. We then use the diffForHumans method to calculate the difference between the two dates and display it in a human-readable format. By using Carbon's diffForHumans method in Laravel, you can easily display time differences in a way that is intuitive and easy for users to understand. This helps improve the user experience of your application by providing clear and concise information about when events occurred.
Lets see how to use diffForHumans() in laravel. Initially we wont pass any parameter if we need to compare two date we need to pass the another date as the parameter like diffForHumans($second_date) in the carbon function .
$post->created_at->diffForHumans() //output : 2 hours ago
$user->created_at->addDays(2)->diffForHumans() // output : 2 days from now
$yesterday->diffForHumans($today) // output : 1 day before
$tomorrow->diffForHumans($today) // output : 1 day after
We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.
Be the first to know about releases and tutorial news and solutions.
We care about your data in our privacy policy.
ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.
Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides