In Laravel 8, How to use
How to create a custom function to get
In Laravel 8,
timesince
is not a built-in function or method. However, it is possible to get the needed result (i.e. get a string representation of how long time ago was) by using one of several methods.
How to get time since a certain moment in the past in Laravel?
- By using Carbon library
diffForHumans()
function (better method) - Create a custom helper function to achieve this functionality.
How to use diffForHumans()
function in Laravel?
diffForHumans()
is a method in the Carbon PHP library that provides a human-readable representation of the difference between two dates. This method is particularly useful when working with dates and times in web applications where you want to display a date in a user-friendly way.
The diffForHumans()
method takes one optional argument, which is the Carbon instance to compare the current instance with. If no argument is provided, it compares the current instance with the current date and time.
The diffForHumans()
method returns a string that represents the difference between the two dates in a human-readable format. For example:
$date = Carbon\Carbon::now()->subDays(1); echo $date->diffForHumans(); // "1 day ago"The string returned by
diffForHumans()
is localized, which means it will be displayed in the language of the current locale.
Here are a few more examples of diffForHumans()
output:
$date = Carbon\Carbon::now()->subDays(7); echo $date->diffForHumans(); // "1 week ago" $date = Carbon\Carbon::now()->addDays(1); echo $date->diffForHumans(); // "1 day from now"Overall, the
diffForHumans()
method provides a convenient way to display dates and times in a human-readable format that is easy for users to understand.
How to create a custom function to get timesince
functionality in Laravel?
In Laravel 8, timesince
is not a built-in function or method. However, it is possible to create a custom helper function to achieve this functionality.
timesince
is typically used to calculate the time elapsed between a given date and the current date and express it in a human-readable format such as "2 days ago", "1 week ago", etc.
Here's an example implementation of a timesince
helper function in Laravel 8:
- Create a new file named
helpers.php
in theapp
directory. - Define the
timesince
function in thehelpers.php
file:
function timesince($datetime) { $now = new \DateTime(); $ago = new \DateTime($datetime); $diff = $now->diff($ago); $interval = $diff->format('%a'); if ($interval == 0) { return 'today'; } elseif ($interval == 1) { return 'yesterday'; } elseif ($interval < 7) { return $diff->format('%a days ago'); } elseif ($interval < 30) { return $diff->format('%W weeks ago'); } elseif ($interval < 365) { return $diff->format('%M months ago'); } else { return $diff->format('%Y years ago'); } }
- Include the
helpers.php
file in thecomposer.json
file:
"autoload": { "files": [ "app/helpers.php" ], },
- Run
composer dump-autoload
command to autoload thehelpers.php
file. - Finally, you can use the
timesince
function in your Laravel 8 application to calculate the time elapsed since a given date:
$datetime = '2022-01-01 00:00:00'; echo timesince($datetime); // Output: 2 months agoNote that this is just one example implementation of a
timesince
function, and you can modify it according to your specific requirements.