我正在使用碳来比较两个日期,最初我使用了两个静态变量,一个是 $Hnow = now()
其二 $Hlim = Carbon :: Create($year, $month, $day, $hourM, $minute, $second);
的比较如下。
$intervalH = $Hnow-> diffInHours ($Hlim);
对我来说,它工作得很完美,但现在我必须把它改编成一个数据库,在那里我有几个时间戳,而不是一个静态的$ Hlim,我有许多日期,这是对工作的限制。我如何将表中的每个日期与当前日期进行比较,并有序地显示这些数据?我明白时间戳不能与碳进行比较,所以我先进行了转换,但它并没有给我显示任何捕获的日期。
控制器。
<?php
namespace App\Http\Controllers;
use App\pamatrixinf;
use App\Periodicity;
use Illuminate\Http\Request;
use App\Alertnotification;
use Carbon\Carbon;
use DateTime;
use App\pawork;
class fen extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$hour= 14;
$minute= 0;
$second=0;
$year= date("Y");
$month= date("m");
$day=date("d");
$hourM=date("H");
$dayy= now();
$dateli=pamatrixinf::select('pamatrixinf.dateen')
->where('pamatrixinf.read_at','=',0,'AND')
->where('codpaarea', '=', auth()->user()->codarea)
->get();
$datelim= strtotime($dateli);
$datedif=Carbon::Create($year,$month,$day,$hourM,$minute,$second);
$dateend= $datedif->addHour();
$intervalH= $dayy->diffInHours($datelim);
$intervalM= $dayy->diffInMinutes($dateend);
return view('fen.index', [
'Works' => Periodicity::select('paperiodicity.descriptionp','pamatrixinf.description', 'pamatrixinf.codpar')
->join('pamatrixinf', 'pamatrixinf.cod_paperiodicity', '=', 'paperiodicity.cod')
->where('pamatrizinfoperio.read_at', '=', 0, 'AND')
->where('pamatrizinfoperio.codpaarea', '=',auth()->user()->codarea)
->get(),
'Periodores' => $intervalH,
'Periodoresm' => $intervalM,
'Hactual' => $dayy,
'Hlimit' => $datelim
]);
}
}
还有我的观点。
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header"><h4>Fenecer</h4></div>
<div class="card-body">
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
Welcome {{Auth::user()->name}}, the work for today is:
@foreach($Works as $work)
<li class="list-group-item">
La informacion por mandar llamada
<p style="color:red;"> <b>{{$trabajo->descripcion}}</b> </p>
@if($Hactual<$Hlimit)
is about to end in
{{$Periodores}} Hours and
{{$Periodoresm}} Minutes, the limit is {{$Hlimit}} and the date actual is {{$Hactual}}.
@elseif($Hactual>$Hlimit)
<p style="color:red;"> <b> It has passed away please we inform the manager. The work died on {{$Hlimit}} and the actual date is {{$Hactual}} </b> </p>
@endif
</li>
@endforeach
</div>
</div>
</div>
</div>
</div>
如你所见,我想做的是在视图中显示小时数的差异($datelim),以便知道每项工作还剩多少时间来完成其交付时间。
改变你的查询和它的下面一行这样,因为你是直接传球的 $dateli
其中包含查询结果和 strtotime
函数期待一个字符串.希望能帮到你。
$dateli=pamatrixinf::select('pamatrixinf.dateen')
->where('pamatrixinf.read_at','=',0,'AND')
->where('codpaarea', '=', auth()->user()->codarea)
->get()
->toArray();
$datelim= strtotime($dateli[0]['dateen']);