Laravel heavily uses Carbon library for date and time management, but there’s one package that extends Carbon to make it work better with local strings.
jenssegers/date has been downloaded over million times already, so for those who don’t know it yet, really recommended.
Easy installation
1. Run command:
composer require jenssegers/date
2. Add “use” statements wherever you want to use it
use Jenssegers\Date\Date;
3. Go on 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 converting local text into Date object:
$date = Date::createFromFormat('l d F Y', 'lunes 9 abril 2018');
But be careful with that – 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 try-catch:
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:
Cloud Software
Berita Olahraga
Lowongan Kerja
Berita Terkini
Berita Terbaru
Berita Teknologi
Seputar Teknologi
Berita Politik
Resep Masakan
Pendidikan
Berita Olahraga
Berita Olahraga