How to humanize a date in PHP

Asked

Viewed 766 times

20

How to humanize a date in PHP? For example, from that date 2015-08-20, I want the function to return to me Há uma semana, because it’s strange to read something like foi publicado há 754 dias or publicado há 38 semanas.

  • 4

    Related: http://answall.com/q/82278/101

  • 1

    @mustache, thanks that I didn’t know

2 answers

22

I know two libraries to make this conversion, one is the Carbon which is a specialization of the default php Datetime and the PHP Humanizer humanizing dates and other information the limitation of it today is that it has location only for English and Polish.

Carbon

Install nfs Carbon on the command line.

composer require nesbot/carbon

Object creation asks for a date and Timezone both are optional, setLocale() is responsible for the translations your call can be made via statistical method and diffForHumans() returns the formatted string of how much time has passed since the initial date.

<?php
require 'vendor/autoload.php';
use Carbon\Carbon;
$data = new Carbon('2015-08-20', 'America/Sao_Paulo');
$data->setLocale('pt_BR');
echo $data->diffForHumans() .PHP_EOL;

$data->addDays(3);
echo $data->diffForHumans() .PHP_EOL;   

Exit:

há 1 semana
há 6 dias 

PHP Humanizer

Installation

composer require coduo/php-humanizer

difference() does the same as diffForHumans() returns an approximate formatted string, such as 1 week ago, preciseDifference() shows the complete difference something like: 20 days, 16 minutes, 52 seconds from now

<?php
require 'vendor/autoload.php';
use Coduo\PHPHumanizer\DateTime;

$data = new \DateTime('2015-08-10');
echo DateTime::difference($data, new \DateTime()) .PHP_EOL;
echo DateTime::preciseDifference($data, new \DateTime()) .PHP_EOL;  

Exit:

3 weeks from now
20 days, 18 minutes, 7 seconds from now 

14

Following the same principle of this post, can from this code:

function RelativeTimeString($timestamp) {
    $minute = 60;
    $hour = $minute * 60;
    $day = $hour * 24;
    $month = 30 * $day;
    $year = 12 * $month;

    $delta = floor(time() - $timestamp);
    if ($delta < 2 )           return 'Agorinha';
    if ($delta < 1 * $minute)  return "Há $delta segundos";
    if ($delta < 2 * $minute)  return 'Há um minuto';
    if ($delta < 45 * $minute) return 'Há '.round($delta / $minute).' minutos';
    if ($delta < 90 * $minute) return 'Há uma hora';
    if ($delta < 23 * $hour)   return 'Há '.round($delta / $hour).' horas';
    if ($delta < 48 * $hour)   return 'Ontem';
    if ($delta < 30 * $day)    return 'Há '.round($delta / $day).' dias';
    if ($delta < 45 * $day)    return 'Há um mês';
    if ($delta < 11 * $month)  return 'Há '.round($delta / $month).' meses';
    if ($delta < 18 * $month)  return 'Há um ano';
    return 'Há '.round($delta / $year).' anos';
}

See working on IDEONE

Notes:

  • For convenience, I kept the parameter as timestamp (in seconds, easier to adapt to any language or implementation). To enter the date in string, just use

    strtotime( $DateString )
    

    instead of

    $timestamp
    

    remembering what dates with / are considered MM/DD/AAAA and with - are considered DD-MM-AAAA. Dates in AAAA-MM-DD are understood correctly with any separator.

  • The time() is based on UTC, if your data is saved at local time (which may not be interesting for this type of application), you need to add the difference:

    $tzseconds = date('Z');
    $delta = floor(time() - $timestamp - $tzseconds);
    
  • Consider that if you have ranges greater than the PHP integer capacity, you need to review the application, as 32-bit integers only cover one span 68 years. This is not a limitation of the function itself, but rather the way integers are stored in PHP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.