简体   繁体   中英

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?

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 .

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"); ?> <?php echo date("FdY"); ?> .

You can just use {{ $variable }} instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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