MouteeSabouni
MouteeSabouni

Reputation: 1

Carbon Laravel class not working properly when shortening seconds, minutes, and hours

My problem is probably simple, instead of getting 1s, 2s, 3s, ... 59s, then 1m, 2m, 3m, ... 59m, then 1h, 2h, ... 23h, 1d. I'm only getting the ones of that, meaning I only get 1s, 1m, 1h. For examply, 1m doesn't turn into 2m, it only turns into 1h after an hour passed. I'm kinda sure it's because of the custom.php file I created. Any help, please?

I wrote this in one of my views:

            @foreach ($project->activity as $activity)
            <li class="mb-1">
                @include("projects.activity.{$activity->action}")
                {{ $activity->created_at->diffForHumans() }}
            </li>
            @endforeach

Then I added the following code to my AppServiceProvider:

public function boot(): void
    {
        Carbon::setLocale(config('app.locale'));

        $translator = Carbon::getTranslator();
        $translator->addResource('array', require resource_path('lang/vendor/Carbon/custom.php'), config('app.locale'));
    }

And this is my custom.php file in resources/lang/vendor/Carbon:

<?php

return [
    'second' => '1s',
    'seconds' => ':count s',
    'minute' => '1m',
    'minutes' => ':count m',
    'hour' => '1h',
    'hours' => ':count h',
    'day' => '1d',
    'days' => ':count d',
    'week' => '1w',
    'weeks' => ':count w',
    'month' => '1mo',
    'months' => ':count mo',
    'year' => '1y',
    'years' => ':count y',
    'ago' => ':time ago',
    'from_now' => ':time from now',
    'after' => ':time after',
    'before' => ':time before',
];

I tried this as well:

        <?php
        use Carbon\Carbon;
        $now = Carbon::now();
        $past = $now->subMinutes(2);

        echo $past->diffForHumans();
        ?>

and it also returned 1m instead of 2m.

Upvotes: 0

Views: 30

Answers (0)

Related Questions