user10665294
user10665294

Reputation:

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.

Upvotes: 2

Views: 5873

Answers (1)

Mozammil
Mozammil

Reputation: 8750

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("F-d-Y"); ?>.

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

Upvotes: 7

Related Questions