On laravel there is some kind of extension that localizes time in the form "Today, at 15:40" "5 minutes ago" and so on

    1 answer 1

    $mytime = Carbon\Carbon::now(); echo $mytime->toDateTimeString(); 

    This is output in the usual Ymd H:i:s format, but there are many possibilities to read at the docks here or here.

    Here is an example:

     echo Carbon::now()->subSecond(5)->diffForHumans(); // 5 секунд назад 

    Install:

     $ composer require nesbot/carbon { "require": { "nesbot/carbon": "~1.14" } } <?php require 'vendor/autoload.php'; use Carbon\Carbon; printf("Now: %s", Carbon::now()); 

    In some assemblies, it is already installed, you just need to register use . There is also an installation section in the documentation .

    More examples:

     echo Carbon::now()->subDays(5)->diffForHumans(); // 5 days ago echo Carbon::now()->diffForHumans(Carbon::now()->subYear()); // 1 year after $dt = Carbon::createFromDate(2011, 8, 1); echo $dt->diffForHumans($dt->copy()->addMonth()); // 1 month before echo $dt->diffForHumans($dt->copy()->subMonth()); // 1 month after echo Carbon::now()->addSeconds(5)->diffForHumans(); // 5 seconds from now echo Carbon::now()->subDays(24)->diffForHumans(); // 3 weeks ago echo Carbon::now()->subDays(24)->diffForHumans(null, true); // 3 weeks 
    • Sorry, but how to install this extension? - das
    • @das changed the answer. - Shadow33
    • Thanks a lot, it all worked. Only one more question remains. For example, in the controller, the date is displayed as it should, but when I try to use it in the template engine I get the error Class 'Carbon' not found - das
    • @das can be transferred to a variable in the template from the controller or connected in the scope of the template. He simply does not see this class and cannot use it. - Shadow33
    • The fact is that I have a brute force array in the template, and for brute force, I need to use it in the template - das