skip to Main Content

I display a diffForHumans with one part

echo Carbon::create('1 hour 59 minutes ago')->diffForHumans(); // 1 hour ago
echo Carbon::create('1 day 23 hours ago')->diffForHumans(); // 1 day ago
echo Carbon::create('1 month 23 days ago')->diffForHumans(); // 1 month ago
echo Carbon::create('1 year 10 months ago')->diffForHumans(); // 1 year ago

For up to 25 months, I’d prefer to see months before it switches to year: e.g. to see 23 months instead of 1 year.

More generaly, I would like to keep the lower unit (like month) when the higher unit (like year) is 1 or 2.

I would also like the display to be as compact as possible.

How do I do that?

2

Answers


  1. Chosen as BEST ANSWER
    use CarbonCarbon;
    use CarbonCarbonInterface;
    
    function diffForHumans(Carbon $carbon)
    {
       return $carbon->diffForHumans([
            'syntax' => CarbonInterface::DIFF_ABSOLUTE, // no ago 
            'short' => true, // e.g. m instead of minutes
            'skip' => $carbon->diffInMinutes() < 3 * 60 ? ['hour'] : (
                $carbon->diffInHours() < 3 * 24 ? ['day'] : (
                    $carbon->diffInDays() < 3 * 7 ? ['week'] : (
                        $carbon->diffInWeeks() < 3 * 4 ? ['month'] : (
                            $carbon->diffInMonths() < 3 * 12 ? ['year'] :
                                []
                        )))),
        ]);
    }
    

    Test:

    function test($carbonString)
    {
        return diffForHumans(
            $c = Carbon::create($carbonString)) . "t" .  
            $c->diffForHumans() ."t". 
            $carbonString . "n";
    }
    
    echo '<pre>';
    
    echo test('1 hour 59 minutes ago');
    echo test('1 day 23 hours ago');
    echo test('1 month 23 days ago');
    echo test('1 year 10 months ago');
    
    echo test('2 minutes ago');
    echo test('3 minutes ago');
    echo test('2 hours ago');
    echo test('3 hours ago');
    echo test('2 days ago');
    echo test('3 days ago');
    echo test('2 weeks ago');
    echo test('3 weeks ago');
    echo test('2 months ago');
    echo test('3 months ago');
    echo test('2 years ago');
    echo test('3 years ago');
    

    The test output is:

    | solution | default carbon | input
    | 119m     | 1 hour ago     | 1 hour 59 minutes ago
    | 47h      | 1 day ago      | 1 day 23 hours ago
    | 7w       | 1 month ago    | 1 month 23 days ago
    | 22mos    | 1 year ago     | 1 year 10 months ago
    | 2m       | 2 minutes ago  | 2 minutes ago
    | 3m       | 3 minutes ago  | 3 minutes ago
    | 120m     | 2 hours ago    | 2 hours ago
    | 3h       | 3 hours ago    | 3 hours ago
    | 48h      | 2 days ago     | 2 days ago
    | 3d       | 3 days ago     | 3 days ago
    | 14d      | 2 weeks ago    | 2 weeks ago
    | 3w       | 3 weeks ago    | 3 weeks ago
    | 8w       | 2 months ago   | 2 months ago
    | 3mos     | 3 months ago   | 3 months ago
    | 104w     | 2 years ago    | 2 years ago
    | 3yrs     | 3 years ago    | 3 years ago
    

  2. This is an update to skip weeks

    function diffForHumans(Carbon $carbon)
    {
       return $carbon->diffForHumans([
            'syntax' => CarbonInterface::DIFF_ABSOLUTE, // no ago 
            'short' => true, // e.g. m instead of minutes
            'skip' => $carbon->diffInMinutes() < 3 * 60 ? ['hour'] : (
                $carbon->diffInHours() < 3 * 24 ? ['day'] : (
                    // $carbon->diffInDays() < 3 * 7 ? ['week'] : (
                        $carbon->diffInWeeks() < 3 * 4 ? ['month','week'] : (
                            $carbon->diffInMonths() < 3 * 12 ? ['year'] :
                                []
                        ))),
        ]);
    }
    

    Test output (same input data):

    | solution | default carbon | input |
    | 119min    | 1 uur geleden | 1 hour 59 minutes ago |
    | 47u   | 1 dag geleden | 1 day 23 hours ago |
    | 54d   | 1 maand geleden   | 1 month 23 days ago |
    | 22mnd | 1 jaar geleden    | 1 year 10 months ago |
    | 2min  | 2 minuten geleden | 2 minutes ago |
    | 3min  | 3 minuten geleden | 3 minutes ago |
    | 120min    | 2 uur geleden | 2 hours ago |
    | 3u    | 3 uur geleden | 3 hours ago |
    | 48u   | 2 dagen geleden   | 2 days ago |
    | 3d    | 3 dagen geleden   | 3 days ago |
    | 14d   | 2 weken geleden   | 2 weeks ago |
    | 21d   | 3 weken geleden   | 3 weeks ago |
    | 62d   | 2 maanden geleden | 2 months ago |
    | 3mnd  | 3 maanden geleden | 3 months ago |
    | 104w  | 2 jaar geleden    | 2 years ago |
    | 3j    | 3 jaar geleden    | 3 years ago |
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search