Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pickab00's avatar

Laravel Carbon Date Difference between two dates

I have a field in my DB which has a time stamp using moment js. The result is like so.

["2018-02-11 11:30:17","2018-02-11 11:20:17","2018-02-11 11:10:17"]

But when i return created_at colum from db the array is given like so:

[{"date":"2018-02-11 11:40:17.000000","timezone_type":3,"timezone":"Asia\/Karachi"},{"date":"2018-02-11 11:40:31.000000","timezone_type":3,"timezone":"Asia\/Karachi"},{"date":"2018-02-11 11:40:40.000000","timezone_type":3,"timezone":"Asia\/Karachi"}]

So how can i take two column dates in a format where carbon can understand? I want the "starttime" column to compare with "created_at". Is this achievable? Here is my code so far:

    $cleanStart = Clean::pluck('starttime')->toArray();
    $cleanFinish = Clean::pluck('created_at')->toArray();

    $from = Carbon::parse($cleanStart);
    $to = Carbon::parse($cleanFinish);
    $diff_in_hours = $to->diffInHours($from);

    return $diff_in_hours; 

But it gives me an error:

Type error: DateTime::__construct() expects parameter 1 to be string, array given

Also how can i give the array to carbon.

0 likes
7 replies
Cinek's avatar

Note, that the $cleanStart variable is an array of dates so you should fetch only one row or calculate date diff for each row.

Example:

$cleanCollection = Clean::get(['starttime','created_at']);
    foreach($cleanCollection as $cleanObj){
        $diff = $cleanObj->created_at->diffInHours($cleanObj->starttime);
    }

You don't need to parse created_at and starttime to Carbon, an Eloquent should do it automatically.

pickab00's avatar

@Cinek thanks did not know that.

I am getting this error now:

Type error: Argument 1 passed to Carbon\Carbon::diffInHours() must be an instance of Carbon\Carbon or null, string given, called in C:\xampp\htdocs\test\app\Http\Controllers\AdminController.php on line 34
Snapey's avatar

Just confirm..

How is your data structured in the database. It looks like you have saved an array of dates?

What is the schema?

Cinek's avatar
Cinek
Best Answer
Level 6

What type is your "starttime" field in your database? I see, that the Eloquent didn't parse starttime to Carbon. You have 2 options:

  1. Parse starttime to Carbon directly in function
$cleanCollection = Clean::get(['starttime','created_at']);
    foreach($cleanCollection as $cleanObj){
        $startTime = Carbon::parse($cleanObj->starttime);
        $diff = $cleanObj->created_at->diffInHours($startTime);
    }
  1. Add to your Clean model a mutator (https://laravel.com/docs/5.5/eloquent-mutators#accessors-and-mutators), that will be parse "starttime" to carbon automatically and first code will work:
public function getStarttimeAttribute($date)
    {
        return Carbon::parse($date);
    }

Notice: if you pass null as parameter to "parse" method, the Carbon will return actual time

2 likes
pickab00's avatar

@Cinek I set it to varchar because i am yet on testing phase. This method works. I could use a mutator but in a later progress. One more thing which confuses me. How come the above code is outputting one value? I am getting the result for one value.

Snapey's avatar

Just confirm..

How is your data structured in the database. It looks like you have saved an array of dates?

What is the schema?

pickab00's avatar

@Snapey I have 3 records in my db at the moment. those timestamps are taken from those 3 records and pulled out as an array

Please or to participate in this conversation.