How to humanize a date in PHP

19

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

    
asked by anonymous 30.08.2015 / 00:19

2 answers

23

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

Carbon

Install nfs carbon by the composer on the command line.

composer require nesbot/carbon

The creation of the object asks for a date and the timezone are both optional, setLocale() is reponsavel by the translations your call can be made via static method and diffForHumans() returns the formatted string of how much time has passed since the start 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;   

Output:

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() displays 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;  

Output:

3 weeks from now
20 days, 18 minutes, 7 seconds from now 
    
30.08.2015 / 00:21
15

Following the same principle of this post , you can start 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 at IDEONE

Notes:

  • For the sake of 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 that dates with / are considered MM/DD/AAAA and - are considered DD-MM-AAAA . Dates in AAAA-MM-DD are understood correctly with any separator.

  • time() is based on UTC, if your data is saved in 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 integer capacity of PHP, you need to revise the application, since 32-bit integers only cover a span of 68 years. This is not a limitation of the function itself, but rather of the way in which integers are stored in PHP.

07.04.2017 / 06:21