Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carbon subtracting Datetime from Carbon::now()

Tags:

php

datetime

I want to use this Carbon function: Carbon::now()->subDays(5)->diffForHumans()

And I need to create the correct integer.

I am loading a string with a Datetime, which I want to subtract in Laravel like this:

 $datetime = $score->created_at;

Then I save the current Time into a variable

 $now = Carbon::now()->toDateTimeString();

This is what I get:

 echo $now . '<br>'; // 2014-07-13 22:53:03
 echo $datetime;     // 2014-07-12 14:32:17

But when I want to subtract one from another I get the following error:

 echo $now - $datetime;

Object of class Carbon\Carbon could not be converted to int

Any help here would be greatly apreciated.

like image 864
LoveAndHappiness Avatar asked Jul 13 '14 23:07

LoveAndHappiness


Video Answer


1 Answers

I know it's a bit late, but this works:

$score->created_at->diffForHumans(\Carbon\Carbon::now())
like image 57
manavo Avatar answered Sep 26 '22 23:09

manavo