Laravel strongly uses the carbon library for date and time management, but there is a package that extends carbon to make it better with local chains.
Jenssegers / Date has already been downloaded more than a million times, so for those who do not yet know, really recommended.
Easy installation
1. Execute the order::
composer require jenssegers/date
2. Add “use” instructions wherever you want to use it
use Jenssegers\Date\Date;
3. Continue and use it::
// First - let's set the locale, to Spanish for example
Date::setLocale('es');
echo Date::now()->format('l j F Y H:i:s');
// lunes 16 abril 2018 00:06:16
echo Date::parse('-2 day')->timespan();
// 2 dias
echo Date::now()->subDays(10)->subMinutes(10)->timespan();
// 1 semana, 3 días, 10 minutos
My favorite feature is to convert the local text to the date date:
$date = Date::createFromFormat('l d F Y', 'lunes 9 abril 2018');
But be careful with this – if your date text is not recognized, you will see this:
InvalidArgumentException A textual month could not be found.
So I would advise to use pass::
try {
$date = Date::createFromFormat('l d F Y', 'lunes 9 appril 2018');
echo $date->timespan();
} catch (\InvalidArgumentException $exception) {
echo 'Could not recognize the date';
}
More links: