How to humanize a date in PHP


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


I know two libraries to do this conversion, one is carbon which is a standard DateTime specialization in php and PHP Humanizer which humanizes dates and other information.


Install nfs carbon from composer on the command line.

composer require nesbot/carbon

The object creation asks for a date and the timezone both are optional, setLocale() is responsible for the translations its call can be done via static method and diffForHumans() returns the formatted string of how much time has passed since the initial date.

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

echo $data->diffForHumans() .PHP_EOL;   


há 1 semana
há 6 dias 

PHP Humanizer


composer require coduo/php-humanizer

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

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;  


3 weeks from now
20 days, 18 minutes, 7 seconds from now 
Scroll to Top