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?
diffForHumans()
function (better method)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.
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:
helpers.php
in the app
directory.timesince
function in the helpers.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'); } }
helpers.php
file in the composer.json
file:"autoload": { "files": [ "app/helpers.php" ], },
composer dump-autoload
command to autoload the helpers.php
file.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 ago
Note that this is just one example implementation of a timesince
function, and you can modify it according to your specific requirements.
It is generally preferable to stick to existing library functions because it decreases the amount of code needed to maintain, and work with and it greatly reduces the costs of onboarding new developers to the project. However if your project needs are specific enough, it is possible to create new helper functions like the one we described above.
Entrepreneur and full-stack web developer capable of multitasking and completing large-scale projects in a short period of time. Founder of moy-razmer.ru and nomadicsoft.io, large experience in e-commerce and various SaaS projects
Custom eCommerce development starts with your needs. No matter what size your business is, crafting killer websites or robust applications with Nomadic Soft is a winning strategy. Our development team will always use the latest tech tools to build your business interactive and engaging web interfaces and applications.