Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

245
Vistas
laravel - Carbon | diffForHumans() shorter version?

I'm trying to figure out how to shorten the output from a diffForHumans method provided by the Carbon library in laravel.

The default format of diffForHumans is something like this:(from documentation)

  • When comparing a value in the past to default now:

    1. 5 months ago

    2. 1 hour ago

But I want the output to be something like:

  1. 1 h
  2. 5 mins
  3. 5 months
  4. 2 years
  5. Just Now

How can I achieve this?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

As per the source code for diffForHumans

/**
 * Get the difference in a human readable format in the current locale.
 *
 *
 * @param Carbon|null $other
 * @param bool        $absolute removes time difference modifiers ago, after, etc
 * @param bool        $short    displays short format of time units
 *
 * @return string
 */
public function diffForHumans(Carbon $other = null, $absolute = false, $short = false) {
    ...
}

to remove modifiers, pass the second parameter as true and to get shortened version of time pass the third parameter as true

Source code at

vendor/nesbot/carbon/src/Carbon/Carbon.php
about 4 years ago · Santiago Trujillo Denunciar

0

Carbon give you an option to remove 'ago'

$time = \Carbon\Carbon::now()->subMinutes(1)->diffForHumans(null, true)

enter image description here

if you need use like '1 h, 5 mins', just str_replace(['hours', 'minutes'], ['h', 'mins'], $time);

for Just now, you need to specify how long is just now.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda