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

marvino's avatar

How to get the duration date and time between two timestamp("create_at")

Hey, I need some help, I want to get the duration or difference of two timestamps in the same column in the database, how to deal with it?

0 likes
19 replies
marvino's avatar

Thanks, another question, how i'm gonna identified between $created_at_A to $created_at_B if they are in the same column in the database.

manelgavalda's avatar

@MARVINO - If they are on the same column I suppose that you are referring to compare 2 different rows

echo $rowA->created_at->diffInDays($rowB->created_at);  
marvino's avatar

Yes exactly, its like a transaction log.

marvino's avatar

@globals i find it hard to solve how I'm gonna identified the rows itself. Can be it identifying the row id? Can you please help me?

manelgavalda's avatar

@MARVINO - Hey, I don't know how do you have your database structured. Show us your users and transactions table schema so we can help you solve your problem.

manelgavalda's avatar

@MARVINO - You need to know which 2 rows you want to compare, for example, if you want to compare the rows with the id 1 and 2. You can do this:

$first = DB::table('transaction')->find(1);
$second = DB::table('transaction')->find(2);

$first->created_at->diffInDays($second->created_at);
marvino's avatar

How about 3 or more ... ? is there a more flexible with this one?

marvino's avatar
@foreach($documents->transaction as $v)
<tr>
     <td>{{$v->created_at}}</td>
     <td>{{$v->office->office}}</td>
     <td>{{$v->remarks}}</td>
     <td>{{$v->status}}</td>
     <td>
      <?php
             $first = DB::table('transactions')->find($v->d_id);
                  $second = DB::table('transactions')->find(2);

   echo  ($first->created_at)->diffInDays($second->created_at);
                  ?>
        </td>
 @endforeach

I get an error call to a member function diffInDays()

manelgavalda's avatar

@MARVINO - It looks like you already have 1 model with a created at, you can do this:

$rowYouWantToCompareWith = Model::find(1)// you need to define the model you want to compare the date against.
echo  $v->created_at->diffInDays($rowYouWantToCompareWith->created_at);

Paste the error code if it is failing.

About comparing 3 or more dates, what do you mean, the dates are compared by sets of 2.

dateA compared with dateB

marvino's avatar

Its running but the rest of the output are the same. It says 25 days but if you subtract the timestamps its different.

marvino's avatar

I got the error

Classs 'Transaction' not found

Here is my code

@foreach($documents->transaction as $v)
 <tr>
<td>{{$v->created_at}}</td>
<td>{{$v->office->office}}</td>
<td>{{$v->remarks}}</td>
  <td>{{$v->status}}</td>
  <td>
       <?php
             $v->o_id = Transaction::find($v->d_id);
                  // you need to define the model you want to compare the date against.
           echo  $v->created_at->diffInDays($v->o_id->created_at);
                                                   // echo $v->id;
                  ?>
             </td>
          </tr>
      @endforeach
manelgavalda's avatar

@MARVINO - define the namespace, use App\Transaction instead

@foreach($documents->transaction as $v)
 <tr>
<td>{{$v->created_at}}</td>
<td>{{$v->office->office}}</td>
<td>{{$v->remarks}}</td>
  <td>{{$v->status}}</td>
  <td>
       <?php
             $v->o_id = App\Transaction::find($v->d_id);
                  // you need to define the model you want to compare the date against.
           echo  $v->created_at->diffInDays($v->o_id->created_at);
                                                   // echo $v->id;
                  ?>
             </td>
          </tr>
      @endforeach
1 like
manelgavalda's avatar

@MARVINO - If this is always returning 25 this might be that you are not fetching the correct dates to compare, I suggest you to print the 2 dates and see if theyre are the dates that you are expecting.

By the way, you don't need to use the php tags, this code will do the same that you're actual code, but it looks cleaner.

@foreach($documents->transaction as $v)
  <tr>
    <td>{{$v->created_at}}</td>
    <td>{{$v->office->office}}</td>
    <td>{{$v->remarks}}</td>
    <td>{{$v->status}}</td>
    <td>{{$v->created_at->diffInDays(App\Transaction::find($v->d_id)->created_at)}}</td>
  </tr>
@endforeach
1 like
marvino's avatar

@globals I print the 2 dates and the same i got. I tried to change the id but its still the same . Perhpas you have a solution on it?

marvino's avatar

@globals The output should be every row can solve the difference of timestamps, in the code, it happens that you declare the first created_at and it just loops to the next rows, that why the same results i get.

 @foreach($documents->transaction as $v)
<tr>
 <td>{{$v->created_at}}</td>
<td>{{$v->office->office}}</td>
<td>{{$v->remarks}}</td>
<td>{{$v->status}}</td>
<td>
<?php 
     $first = App\Transaction::find($v->id);
    $second = App\Transaction::find($v->o_id);

    echo $first->created_at, $second->created_at; // try to look the output
                                                 ?>
     {{$v->created_at->diffForHumans(App\Transaction::find($v->o_id)->created_at)}}
</td>
</tr>
@endforeach

Please or to participate in this conversation.