0

How I can get string "today, 12:26" with Carbon in Laravel?
I have date: "2017-12-17 12:26", How can I get these formats?

1) Today, 12:26,
2) 1 hour ago
3) 1 min ago
3) 1 sec ago
4) Yesterday, 12.26
5) 17 december, 12:26

Machavity
  • 30,841
  • 27
  • 92
  • 100
Jadasdas
  • 869
  • 4
  • 19
  • 39
  • t should not be that difficult to find this out yourself. – robbyrr Dec 17 '17 at 13:50
  • Many question available with strtotime, you can change strtotime to carbon and use as you want , [1st](https://stackoverflow.com/questions/25622370/php-how-to-check-if-a-date-is-today-yesterday-or-tomorrow), [2nd](https://www.geekshangout.com/php-relative-date-function-today-yesterday-2-days-ago-in-3-days-etc/) – Niklesh Raut Dec 17 '17 at 14:02

2 Answers2

1

You should look into diffForHumans method in Carbon. For example:

Carbon::now()->subDays(1)->diffForHumans()
//1 day ago

Carbon::now()->diffForHumans()
//1 second ago

Carbon::now()->subDays(25)->diffForHumans();
// 3 weeks ago

Edit:

The fact that you are using Laravel, note that you can call diffForHumans like this too:

$user->created_at->diffForHumans();

Hyder B.
  • 10,900
  • 5
  • 51
  • 60
1

There is a similar thing in Carbon, the ->diffForHumans(); method. Example:

echo Carbon::now()->subDays(1)->diffForHumans();

Produces:

1 day ago

You can always refer to Carbon documentation it is very well-documented. Carbon Documentation.

ndrwnaguib
  • 5,623
  • 3
  • 28
  • 51