Search code examples
phplaravelphp-carbon

Laravel Carbon DiffForHumans Option (Years, Months, Days)


I have read through the documentation of DiffForHumans and it's very straightforward, but is there a way to customize the date to print out:

  • 13 Years, 1 Month, 10 Days
  • 01 Year, 11 Months, 01 Day

where it will respect the Year/Years, Month/Months, Day/Days?

  • Approach 1:

    $date = Carbon::parse('2010-08-29');
    $formattedDate = $date->diffForHumans(['parts' => 3, 'join' => ', '];
    

    Will print out 13 years, 1 month, 4 weeks ago

  • Approach 2:

    $date = Carbon::parse('2010-08-29');
    $formattedDate = $date->diff(now())->format('%y Year, %m Months and %d Days')
    

    Will print out 13 Year, 1 Months and 28 Days

  • Approach 3 (Backup option):

    $date = Carbon::parse('2010-08-29');
    $formattedDate = $date->diff(now())->format('%y Year(s), %m Month(s) and %d Day(s)')
    

    Will print out 13 Year(s), 1 Month(s) and 28 Day(s)

Is there a vanilla PHP class that could accomplish something similar?


Modified Accepted Solution:

public function customDiffForHumans(Carbon $date) : string
    {
        $diff = $date->diff(now());

        $years  = $diff->y ? $diff->y . ' Year' . ($diff->y > 1 ? 's' : '') : '0 Years';
        $months = $diff->m ? $diff->m . ' Month' . ($diff->m > 1 ? 's' : '') : '0 Months';
        $days   = $diff->d ? $diff->d . ' Day' . ($diff->d > 1 ? 's' : '') : '0 Days';

        return implode(', ', array_filter([$years, $months, $days]));
    }

Solution

  • That's the custom function for handling same problem:

    function customDiffForHumans(DateTime $date) {
        $now = new DateTime();
        $diff = $date->diff($now);
    
        $years = $diff->y ? $diff->y . ' Year' . ($diff->y > 1 ? 's' : '') : null;
        $months = $diff->m ? $diff->m . ' Month' . ($diff->m > 1 ? 's' : '') : null;
        $days = $diff->d ? $diff->d . ' Day' . ($diff->d > 1 ? 's' : '') : null;
    
        return implode(', ', array_filter([$years, $months, $days]));
    }
    
    $date = new DateTime('2010-08-29');
    return customDiffForHumans($date);