【问题标题】:Laravel How to set default time as today in daterange filter and how to display the data?Laravel 如何在日期范围过滤器中将默认时间设置为今天以及如何显示数据?
【发布时间】:2026-02-10 18:15:02
【问题描述】:

您好,我想将我的 daterange 过滤器的值设置为像今天这样的实时是 2021 年 4 月 5 日,所以我的 daterange 过滤器的默认值变成了从 2021 年 4 月 5 日到 2021 年 4 月 5 日,并且还显示了我的数据库中的数据,应该我愿意吗?

这是我的视图刀片:

  <form action="searchdateinvoice" method="get">
        @csrf
        <div class="container">
            <div class="row">
                <div class="container-fluid">
                    <div class="form-group row">
                        <label for="date" class="col-form-label col-sm-1" style="width: 99px">Date From</label>
                        <div class="col-sm-3">
                            <input type="date" class="form-control input-sm"  value="2021-04-05"name="dateFrom" id="dateFrom" required>
                        </div>
                        
                        <label for="date" class="col-form-label col-sm-1">Date To</label>
                        <div class="col-sm-3">
                            <input type="date" class="form-control input-sm "  value="2021-04-05"name="dateTo" id="dateTo" required>
                        </div>
                        <div class="col-sm-3">
                            <button type="submit" class="btn btn-primary pl-4 pr-4 rounded-pill" style="background-color: #B10000" title="searchdate"> <span
                                    class="pl-1">Filter</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    
 @if (count($indexInvoices) > 0)
        <table class="table table-bordered table-hover" class="display" cellspacing="0">
            <thead style="background-color: #B10000">
                <tr>
                    <th class="text-center text-light" scope="col">No.</th>
                    <th class="text-center text-light" style="width:10%" scope="col">Sale Date</th>
                    <th class="text-center text-light" scope="col">Invoice</th>
                    <th class="text-center text-light" scope="col">Sender</th>
            <th class="text-center text-light" scope="col">Status</th>
                </tr>
            </thead>
            <tbody>
                @foreach ($indexInvoices as $invoice)
                        <tr>
                            <th class="align-middle" scope="row">{{ $loop->iteration }}</th>
                            <th class="align-middle" scope="row">
                                @php
                                    $old_date = explode('-', $invoice->saledate);
                                    $new_data = $old_date[2] . '-' . $old_date[1] . '-' . $old_date[0];
                                    echo $new_data;
                                @endphp
                            </th>
                            <th class="align-middle" scope="row">{{ $invoice->saleno }}</th>
                            <th class="align-middle" scope="row">{{ $invoice->chkby }}</a></th>
                            <th class="align-middle" scope="row">
                                @if ($invoice->isposted == false)
                                    @php
                                        echo 'Pending';
                                    @endphp
                                @else
                                    @php
                                        echo 'Finished';
                                    @endphp
                                @endif
                        </tr>
                    @endif
                @endforeach
            </tbody>
        </table>
        {{ $indexInvoices->links('pagination::bootstrap-4') }}

这是我的发票控制器:

    public function searchDateInvoice(Request $request)
    {
        $fromDate = $request->get('dateFrom');
        $toDate = $request->get('dateTo');
        if(!empty($fromDate))
        {
            $searchDateInvoice = Invoices::whereBetween('saledate', [$fromDate, $toDate])
                        ->orderby('saleno', 'desc')
                        ->paginate(15);
            $searchDateInvoice->appends(['dateFrom'=>$fromDate,'dateTo'=>$toDate]);
        }
        return view('invoices.searchdateinvoice')->with(['searchDateInvoice'=>$searchDateInvoice]);
    }

这是我的路线 (web.php)

Route::get('/invoices','App\Http\Controllers\InvoiceController@indexInvoice');                                                                                                                 
Route::get('/searchInvoice','App\Http\Controllers\InvoiceController@searchInvoice')->name('searchInvoice');   
Route::get('/searchdateinvoice','App\Http\Controllers\InvoiceController@searchDateInvoice')->name('searchdateinvoice');

【问题讨论】:

  • 在 value="=date('Y-m-d')?>" 中使用这样的php日期函数希望能解决你的问题。编辑你能解释一下你想显示什么类型的数据吗

标签: javascript php html laravel


【解决方案1】:

使用 date() 以您想要的任何格式获取当前日期。

<div class="col-sm-3">
    <input type="date" class="form-control input-sm"  value="{{date('Y-m-d')}}"name="dateFrom" id="dateFrom" required>
</div>
                        
<label for="date" class="col-form-label col-sm-1">Date To</label>
<div class="col-sm-3">
    <input type="date" class="form-control input-sm "  value="{{date('Y-m-d')}}"name="dateTo" id="dateTo" required>
</div>

sedond部分的小变换,也可以使用日期

@php
    echo date('d-m-Y', strtotime($invoice->saledate));
@endphp

// or just

{{date('d-m-Y', strtotime($invoice->saledate))}}

【讨论】:

  • 感谢设置默认时间的解决方案,但是我怎样才能根据默认时间(今天)自动显示数据?
  • @NewbieProgrammer 就是这样做的,它将默认设置为今天。除非我不明白你的意思。明天date('d-m-Y') 将返回 06-04-2021。
  • 哦,对不起,我的错,您的解决方案是正确的。 :)