简体   繁体   English

列出本月或拉拉韦尔每个月的所有日子

[英]List all days for this month or every month in Laravel

How can I list all days for this month? 如何列出该月的所有日子? I need to list from January 1 to January 31, 2019. How can I list that days to the table? 我需要从2019年1月1日至2019年1月31日列出。我如何在表格中列出那几天?

Currently, I have this: 目前,我有这个:

<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">&nbsp;&nbsp;SET TIME</i>',['type' => 'submit','class' => 'btn btn-warning btn-sm',  'style'=>"display: inline-block;"])}}</td>     
        </tr>
    </tbody>
</table>  

For now, the output of my current code is just a single date that updates every day, how can I list all the days for this month? 现在,我当前代码的输出只是每天更新的单个日期,如何列出该月的所有日期? Or for February? 还是二月?

My expected output is to list all days in this month. 我的预期输出是列出本月的所有日子。 and the other month. 和另一个月。

Since you are using Laravel, you should be able to use Carbon . 由于您正在使用Laravel,因此应该可以使用Carbon

So, the following should make it possible in your case. 因此,以下情况将使您成为可能。

@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">&nbsp;&nbsp;SET TIME</i>',['type' => 'submit','class' => 'btn btn-warning btn-sm',  'style'=>"display: inline-block;"])}}</td>
            </tr>
        @endforeach
    </tbody>
</table>

Obviously, the logic for computing the dates should probably be moved to a Controller. 显然,用于计算日期的逻辑可能应该移至控制器。 This is just a simple example. 这只是一个简单的例子。

Also, since you are using Laravel, and I am assuming Blade, there's absolutely no need to use <?php echo date("FdY"); ?> 另外,由于您使用的是Laravel,并且我假设使用Blade,因此绝对不需要使用<?php echo date("FdY"); ?> <?php echo date("FdY"); ?> . <?php echo date("FdY"); ?>

You can just use {{ $variable }} instead. 您可以只使用{{ $variable }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM