Calculate difference in minutes between created_at and current time in Laravel - laravel

I want to show in a field the time someone is waiting, and I want to do that by subtracting created_at time by current time in minutes
this is my table
<thead>
<tr>
<th scope="col">Ordernummer</th>
<th scope="col">E-mail</th>
<th scope="col">Kenteken</th>
<th scope="col">Wachttijd</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
#foreach($orders as $order)
<tr>
<th scope="row">{{ $order->ordernumber }}</th>
<td scope="row">{{ $order->email }}</td>
<th scope="row">{{ $order->numberplate }}</th>
<td scope="row">{{ $order->created_at }}</td>
<td scope="row">
#if( $order->status == 0)
Open
#else
Afgehandeld
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
And where this is written <td scope="row">{{ $order->created_at }}</td>
I want to show there the difference in minutes.
Thanks already

I assume your $order->created_at is Carbon instance, so you can use:
<td scope="row">{{ $order->created_at->diffInMinutes(\Carbon\Carbon::now()) }} minutes ago</td>
if $order->created_at is pure mysql format not Carbon instance just convert/create it:
\Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $order->created_at)->diffInMinutes(\Carbon\Carbon::now())

You can use Carbon to calculate difference in minutes like so
<td scope="row">{{ now()->diffInMinutes($order->created_at) }}</td>

you can use Carbon::diffInMinutes to get the minutes differnt,
or you can use Carbon::diffForHumans to get more readable statement
$order->created_at->diffInMinutes(Carbon::now());
// that would be: 5
$order->created_at->diffInMinutes(Carbon::now());
and this would be: 5 minuets ago

Related

Laravel 9 Calculate minute difference same_id

I have a column with id and a column with timestamp and a state column, every time the state changes it writes a time in the timestamp column, I want a calculation of the time difference between the various states, for example if it is less than 15 minutes it is ok, if more than 15 minutes is not ok, how can I do to automate it?
it works perfectly but now i need to specify the same same_id to not count all of the table but only the time referring to the same same_id
<table>
<thead>
<tr>
<th>id</th>
<th>timestamp</th>
<th>states</th>
<th>same_id</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2023-01-11 15:26:23</td>
<td>NotAvailable</td>
<td>80</td>
<td>2</td>
<td>2023-01-11 15:26:55</td>
<td>ToBeAssigned</td>
<td>80</td>
<td>3</td>
<td>2023-01-11 15:27:06</td>
<td>Assigned</td>
<td>80</td>
<td>3</td>
<td>2023-01-11 15:27:19</td>
<td>TakingCharge</td>
<td>80</td>
<td>4</td>
<td>2023-01-11 15:29:05</td>
<td>Closed</td>
<td>80</td>
</tr>
</tbody>
</table>
If you use Carbon, you should take a look at diffInMinutes method (read more about that here)
Example:
echo (new Carbon\Carbon('2023-01-11 15:26:23'))->diffInMinutes((new Carbon\Carbon('2023-01-11 15:28:55'))); // 2
If you want to do it with plain PHP, please have a look at this answer: https://stackoverflow.com/a/12382882/14714168
Update:
With the data you provided, I think one way to get it is the following:
<table>
<thead>
<tr>
<th>id</th>
<th>timestamp</th>
<th>states</th>
<th>delay</th>
</tr>
</thead>
<tbody>
#foreach((array)$tickets_logs as $i => $ticket_log)
<tr>
<td>{{ $ticket_log['id'] }}</td>
<td>{{ __($ticket_log['states']) }}</td>
<td>{{ $ticket_log['time_stamp'] }}</td>
<td>
#if(!$loop->first)
{{ (new Carbon\Carbon($tickets_logs[$i - 1]['time_stamp']))->diffInMinutes((new Carbon\Carbon($ticket_log['time_stamp']))) }}
#else
-
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
read more about loop variables here

sum in blade how to do it for the subtotal of two tables?

i found out a way by someone asking a question, of a way of producing subtotals, i now just wanted to sum values between two tables , in the code from the person i tested and it works perfectly, i just wanted to aggregate more one table to any of the querys , it's summing on the blade.
//resumed controller
public function index() {
$emphayas = DB::select('select * from emphayas');
$treasuries = DB::select('select * from treasuries');
return view('database',['emphayas'=>$emphayas,
'treasuries'=>$treasuries,
]);}
-----------------------------------------------------------------
//resumed blade
<table class=" ">
<tr>
<th class="py-3 bg-cyan-400">Total</th>
<td class="py-3 bg-cyan-400">available_for_use_month</td>
</tr>
#foreach ($emphayas as $emphaya)
<tr>
<td></td>
<td>{{ $emphaya->available_for_use_month}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{ \App\Models\Emphaya::sum('available_for_use_month') }}
</tr>
</table>
<table class=" ">
<tr>
<th class="py-3 bg-cyan-400">Total</th>
<td class="py-3 bg-cyan-400">available_for_use_month</td>
</tr>
#foreach ($treasuries as $treasury)
<tr>
<td></td>
<td>{{ $treasury->available_for_use_month}}</td>
</tr>
#endforeach
<tr>
<td>Total</td>
<td>{{ \App\Models\Treasury::sum('available_for_use_month') }}
</tr>
</table>
in this case what i want to do is connect the total of treasury to emphyas
<tr>
<td>Total</td>
<td>{{ \App\Models\Emphaya::sum('available_for_use_month') + \App\Models\Treasury::sum('available_for_use_month') }}
</tr>

Please assist i want to loop through two database tables and view them in a blade.I want records to appear according to dates with in different column

All i want is to show debit and credit according to dates. I want debit to show only if there's new record added and also credit to show only if there's changes in the accounts. I have used observer to check if column is dirty.
here is my blade
<table class="table table-striped">
<thead style="background-color: #0b8a83;color:rgb(0, 0, 0);">
<th>Date</th>
<th>Description</th>
<th>Credit</th>
<th>Debit</th>
<th>Balance</th>
</thead>
#foreach ($monthlyInst as $keyy)
<tr>
<td>{{ date('d/M/y', strtotime($keyy->updated_at)) }}</td>
<td></td>
<td></td>
<td>R {{ $keyy->Installment }}</td>
<td></td>
</tr>
#endforeach
#foreach ($history as $account)
<tr>
<td>{{ date('d/M/y', strtotime($account->updated_at)) }}</td>
<td>{{ $account->accountname }}</td>
<td>R {{ $account->newpaid }}</td>
<td></td>
<td>R {{ $account->newbal }}</td>
</tr>
#endforeach
<tr>
<td></td>
<td>Outstanding Balance</td>
<td></td>
<td></td>
<td><b>R {{ $totalbal }}</b></td>
</tr>
</table>

Nested Looping in Laravel Blade Templating

Here is my Controller
When I run this controller code I get the result as in the screenshot below, as all data is correct.
$query = DB::table('intis_auctions_lots')
->select('bidincrement','intis_lot_id','lot','product_name','maxbid','enddate')
->where([['community_name','=',$CommunityNo],['SellerId','=',$SellerId],['active','=','Y'],['startdate','<',$now],['enddate','>',$now]])
->orderBy('intis_lot_id')
->get();
foreach($query as $querys) {
$qintis_lot_id = $querys->intis_lot_id;
$qenddate = date('H:i', strtotime($querys->enddate));
$individualbids = DB::table('intis_auctions_lots')
->select(DB::raw('sum(nrbids) as nbid,lot, startprice,intis_lot_id,product_name,enddate,maxbid'))
->where([['community_name','=',$CommunityNo],['SellerId','=',$SellerId],['active','=','Y'],['startdate','<',$now],['enddate','>',$now],['intis_lot_id','=',$qintis_lot_id]])
->groupBy('startprice','intis_lot_id','product_name','lot','enddate','maxbid')
->get();
foreach($individualbids as $key => $value)
echo "<tr><td>".$value->lot."</td>
<td>".$value->intis_lot_id."</td>
<td>".$value->product_name."</td>
<td>".date('H:i', strtotime($value->enddate))."</td>
<td>".$value->startprice."</td>
<td>".$value->maxbid."</td>
</tr>";
}
}
My View code as below
<div class="table-responsive">
<table id="zero_config" class="table table-striped table-bordered">
<thead>
<tr>
<th>Lot</th>
<th>Product Name</th>
<th>End Time</th>
<th>Bids</th>
<th>Start Price</th>
<th>Current Bid Amount</th>
<th>Name</th>
</tr>
</thead>
<tbody>
#foreach ($query as $key=>$value)
#foreach ($individualbids as $key => $value1)
<tr>
<td>{{ $value->lot }}</td>
<td style="text-align:left">{{ $value->intis_lot_id }},{{ $value->product_name }}</td>
<td>{{ date('H:i', strtotime($value->enddate)) }}</td>
<td></td>
<td>{{ $value1->startprice }}</td>
<td>{{ $value->maxbid }}</td>
<td></td>
<td></td>
</tr>
#endforeach
#endforeach
</tbody>
<tfoot>
<tr>
</tr>
</tfoot>
</table>
</div>
When pass it to the view I'm getting result as in the screen below, as the Starting Price as 340 it takes the first value to all the row as same value, but when I run in the controller code without passing to view it show correct value. Please provide me the solution to write the proper code in the view, as it will helpful to me.

Laravel Blade fails in forelse, works in foreach

I am upgrading Laravel from 5.2 to 5.3 and one of my Blade views is no longer working. I am passing an array to an included view to loop through it. I am using the forelse directive but it keeps giving me an Undefined offset: 1 error.
Here is the controller snippet with the view call:
$transactions = $committees->transactions()
->where('FiledDate', '<=', $to) // Upper date
->where('FiledDate', '>=', $from) // Lower date
->get();
return view('committees.show',
[
'data' => $data,
'transactions' => $transactions,
]);
Here is the Blade file.
<table class="table table-striped">
<thead>
<tr><th class="text-center" colspan="5">Transactions</th></tr>
<tr>
<th class="text-center">TranId</th>
<th class="text-center">Tran Date</th>
<th class="text-center">SubType</th>
<th class="text-center">Filed Date</th>
<th class="text-center">Amount</th>
</tr>
</thead>
<tbody>
#forelse ($transactions AS $transaction)
<tr>
<td class="text-center">{{ $transaction->TranId }}</td>
<td class="text-center">{{ $transaction->TranDate }}</td>
<td class="text-center">{{ $transaction->SubType }}</td>
<td class="text-center">{{ $transaction->FiledDate }}</td>
<td class="text-center">{{ number_format($transaction->Amount, 2, '.', ',') }}</td>
</tr>
#empty
<tr><td colspan="5">No Transactions</td></tr>
#endforelse
</tbody>
I have created a dummy transaction array but I still got the same error.
Also, when I use the foreach directive it works fine but then I have to have an additional test of checking for no records.
It's not the execution of your iteration that fails, but rather the compiling of your Blade template. The difference is crucial for debugging the issue. Up until de 18th of September the compiling of forelse directives was case-sensitive, meaning that when it tried to compile your statement, it failed to produce the matches it needed to output the actual PHP code you needed. Updating Laravel should fix the issue.
So, to clarify, this would break:
#forelse ($transactions AS $transaction)
While this would work:
#forelse ($transactions as $transaction)
Having said that, I'd strongly advise you to follow PSR-2 and write all PHP keywords in lowercase.
Have you tried vardumping the contents of transaction? It might not have the fields you are accessing in your forelse loop, thereby causing the error.
Try doing a {{var_dump($transaction)}} in your forelse loop and make sure you have all the fields you are accessing in your code.
in controller create $transaction in this way:
$transacrion not array:
use this code:
$transaction = Transaction::all();
You just need to update your #forelse section like I mentioned below:
#forelse ($transactions ? $transactions : [] as $transaction)
<tr>
<td class="text-center">{{ $transaction->TranId }}</td>
<td class="text-center">{{ $transaction->TranDate }}</td>
<td class="text-center">{{ $transaction->SubType }}</td>
<td class="text-center">{{ $transaction->FiledDate }}</td>
<td class="text-center">{{ number_format($transaction->Amount, 2, '.', ',') }}</td>
</tr> #empty
<tr><td colspan="5">No Transactions</td></tr> #endforelse

Resources