Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AliShahbaz's avatar

How can i add a date and Time function ?

Hi guys i'm new in laravel and i want to add a Date and Time function in my code using Ajax or JQuery. Please look at me code and then tell me where should i change it.

In the index.blade.php my form code is this:

<div class="form-horizontal" style="margin-bottom: 15px;">
    <form action="{{ url('date/{id}','[ $id ]') }}" method="get">
        <div class="form-inline" style="margin-left: -20px;">
            <div class="col-md-2 col-lg-2">
                <select id="searchBy" name="searchBy" class="form-control btn btn-secondary">
                 <option selected>Choose a filter</option>
                 <option id="1" value="ThisWeek" >This Week</option>
                 <option id="2" value="ThisMonth"> This Month</option>
                 <option id="3" value="ThisYear"> This Year</option>
                 <option id="4" value="CustomPeriod"> Custom</option>
                </select>
            </div>
            <div id="CustomSearchFields" style="display:none;">
            <div class=" col-lg-3 col-md-3">
                <div class="input-group date" id='startDate'>
                    <span class="input-group-addon">From</span>
                    <input type="text" id="startDateTextBox" class="form-control" name="startDate">
                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                </div>
            </div>

            <div class="col-lg-3 col-md-3">
                <div class="input-group date" id='endDate'>
                    <span class="input-group-addon">To</span>
                    <input type="text" id="endDateTextBox" class="form-control" name="endDate">
                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                </div>
            </div>
        </div>
        </div>
        <div class="col-lg-1 col-md-1 adminTaskIndexSeachBtn">
            <button class="btn btn-primary" type="submit">Search <span class="glyphicon glyphicon-search"></span></button>
        </div>
    </form>
</div>

and i made a Route also in the web.php the route code is this:

Route::get('date/{id}',"PostController@date");

The Ajax function that i made for the date.

$(document).ready(function (){ var selectedSearchBy = $("#selectedSearchBy").val(); switch(selectedSearchBy){ case 'ThisWeek': $('select>option:eq(0)').prop('selected', true); break; case 'ThisYear': $('selecte>option:eq(2)').prop('selected', true); break; case 'CustomPeriod': var startDatePostBack = $("#startDatePostBack").val(); $("#startDateTextBox").val(startDatePostBack); var endDatePostBack = $("#endDatePostBack").val(); $("#endDateTextBox").val(endDatePostBack); $('select>option:eq(3)').prop('selected', true); break; default: $('select>option:eq(1)').prop('selected', true); } });

var selectedOption = $("#searchBy").val(); if (selectedOption == "CustomPeriod") { $("#CustomSearchFields").show(); }

jQuery('#startDate').datetimepicker({ format: 'MM-DD-YYYY', maxDate: moment().endOf('day'), defaultDate: moment() }).on("dp.change", function (e) { jQuery('#endDate').data("DateTimePicker").minDate(e.date); });

jQuery('#endDate').datetimepicker({ format: 'MM-DD-YYYY', maxDate: moment().endOf('day'), defaultDate: moment() }).on("dp.change", function (e) { jQuery('#startDate').data("DateTimePicker").maxDate(e.date); });

And PostController for my index.blade.php is this :

public function date($id){

  $date = Post::find($id);
  return view('posts.index');
}

please look my code and tell me what should i do. What i'm try to do is when user click the This Week / This Month / This Year the record show to relative to it. please help me to fix this i'm very Thankful to you all.

0 likes
2 replies
Snapey's avatar

Duplicate question

You are just adding more code where you clearly don't have a clue.

Strip it back and add bit by bit... You are not ready to start making ajax calls.

Start with some real basic crud operations. Seems to me like you have gone cut and paste crazy without knowing what you are trying to build

1 like
AliShahbaz's avatar

i understand what are you trying to say, but that the reason i asked you guys to help me solve it.

Please or to participate in this conversation.