I have two time values that I want to take difference of. First is coming from a database, and it looks like "2014-05-13 10:41:09". It's in "Y-m-d H:i:s" format. But it's a Carbon object.

I have another time, which is now. I get it like date('Y-m-d H:i:s');

I want to take the difference of those two times. First, I wanted to get now() in Carbon. But I can't. I don't know why. So I tried to transform my Carbon object to PHP Date object. I couldn't either.

But I dumped and died the Carbon object. It looks like this:

object(Carbon\Carbon)#225 (3) { ["date"]=> string(19) "2014-05-13 10:41:09" ["timezone_type"]=> int(3) ["timezone"]=> string(15) "Europe/City" }

The date I want to access is in ["date"] part.

How can I take the difference of those two times?

This is the output of those two days:

2014-05-13 10:46:09

2014-05-13 10:46:20

EDIT:

Here's the output of:

var_dump($unit->heartbeat);
die;

object(Carbon\Carbon)#225 (3) { ["date"]=> string(19) "2014-05-13 10:59:09" ["timezone_type"]=> int(3) ["timezone"]=> string(15) "Europe" }

Thank you very much.

有帮助吗?

解决方案

First, I wanted to get now() in Carbon. But I can't. I don't know why.

The Instantiation section of the Carbon manual provides these examples:

$carbon = new Carbon();                  // equivalent to Carbon::now()
$now = Carbon::now();

They're equivalent, so you have to pick one rather than using both.

You already have a powerful dedicated data type for dates—you should make use of it. Thus this:

date('Y-m-d H:i:s');

... serves no real purpose and only adds confusion.

As about getting differences, there's a whole Difference chapter in the manual. It should be pretty straightforward once you use the appropriate types.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top