[英]List all days for this month or every month in Laravel
如何列出該月的所有日子? 我需要從2019年1月1日至2019年1月31日列出。我如何在表格中列出那幾天?
目前,我有這個:
<table class="table">
<thead>
<tr>
<th>DATE TODAY</th>
<th>TIME IN</th>
<th>TIME OUT</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
<tr>
<td><b><?php echo date("F-d-Y"); ?></b></td>
<!---Date Hidden--> <input type="hidden" name="dateToday" value='<?php echo date("F-d-Y") ?>'>
<td><input type="time" name="timeIn" class="form-control col-md-10"></td>
<td><input type="time" name="timeOut" class="form-control col-md-10"></td>
<td> {{Form::button('<i class="fa fa-clock"> SET TIME</i>',['type' => 'submit','class' => 'btn btn-warning btn-sm', 'style'=>"display: inline-block;"])}}</td>
</tr>
</tbody>
</table>
現在,我當前代碼的輸出只是每天更新的單個日期,如何列出該月的所有日期? 還是二月?
我的預期輸出是列出本月的所有日子。 和另一個月。
由於您正在使用Laravel,因此應該可以使用Carbon 。
因此,以下情況將使您成為可能。
@php
$today = today();
$dates = [];
for($i=1; $i < $today->daysInMonth + 1; ++$i) {
$dates[] = \Carbon\Carbon::createFromDate($today->year, $today->month, $i)->format('F-d-Y');
}
@endphp
<table class="table">
<thead>
<tr>
<th>DATE TODAY</th>
<th>TIME IN</th>
<th>TIME OUT</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
foreach($dates as $date)
<tr>
<td><b>{{ $date }}</b></td>
<input type="hidden" name="dateToday" value="{{ $date }}">
<td><input type="time" name="timeIn" class="form-control col-md-10"></td>
<td><input type="time" name="timeOut" class="form-control col-md-10"></td>
<td> {{Form::button('<i class="fa fa-clock"> SET TIME</i>',['type' => 'submit','class' => 'btn btn-warning btn-sm', 'style'=>"display: inline-block;"])}}</td>
</tr>
@endforeach
</tbody>
</table>
顯然,用於計算日期的邏輯可能應該移至控制器。 這只是一個簡單的例子。
另外,由於您使用的是Laravel,並且我假設使用Blade,因此絕對不需要使用<?php echo date("FdY"); ?>
<?php echo date("FdY"); ?>
。
您可以只使用{{ $variable }}
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.