V
V
Vic Shostak2016-05-22 13:35:52
PHP
Vic Shostak, 2016-05-22 13:35:52

How to display the current date, in the format "1 hour ago", "two weeks ago", etc.?

Greetings!
Can you please tell me a PHP class for displaying a date in a format like VK, depending on a pre-set date? It is desirable that he be friends with the Russian language / cases.
That is, not "May 22, 2016, 13:00", but "30 minutes ago" (and so on).
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrzej Wielski, 2016-05-22
@vikkyshostak

Use https://github.com/briannesbitt/Carbon
Namely, you need the diffForHumans method. Under carbon there are a bunch of locales and translations, you will find it for any language.
In addition, you will have to work with dates more than once, include Carbon in your projects anyway. Will always come in handy.

D
DarkMatter, 2016-05-22
@darkmatter

This is done not in PHP, but in JS, the usual date is displayed in php, JS formats it by replacing the contents of the block with a new one

M
marrs, 2016-05-22
@marrs

//Текущее время
$now = new DateTime('now');
$timestamp_now = $now->getTimestamp();

//timestamp последнего посещения из БД
// $row - это строка из БД
$timestamp = $row['timestamp'];

// $result переводите в нужный формат
$result = $timestamp_now - $timestamp;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question