How To Get Yesterday Today And Tomorrow Date Using Javascript In PHP

admin_img Posted By Bajarangi soft , Posted On 03-10-2022

We can get today, yesterday and Tomorrow date and time by any country name using controller, javascript.

How To Get Yesterday Today And Tomorrow Date Using Javascript In PHP

Step 1:Create Blade file like date.balde.php and make jquery and ajax code for getting today yesterday and tomorrow date and time
resources/views/date.blade.php

    <html>
    <head>
        <title>Age Calculator</title>
        <link rel='stylesheet' href='style.css'/>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-latest.min.js"></script>
        <script src='script.js'></script>

    </head>
    <br>
    <body>
        <div id="main_container" class="container">
            <div class="row">
                <div class="col-md-6">
                    <button id="calculate" class="btn btn-success" style="margin-top: 25px;">Date</button>
                </div>
            </div>
            <br>
            <div class="row">
                <div class="col-md-6">
                    <div id="age_container"><span id="exact" style="color: #3495e3"></span></div>
                    <div id="age_container"><span id="exact1" style="color: #3495e3"></span></div>
                    <div id="age_container"><span id="exact2" style="color: #3495e3"></span></div>

                </div>
            </div>
        </div>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#calculate").click(function(){
                    var date=$("#date").val();
                    $.ajax({
                        type:"POST",
                        url: '{{url('caldate')}}',
                        data:  {"_token": "{{ csrf_token() }}",
                            date   : date},
                        dataType: 'json',
                        success: function (data) {
                            $('#exact').text('Yesterday Date is : ' +data.yesterday);
                            $('#exact1').text('Today Date is: ' +data.today);
                            $('#exact2').text('Tomorrow Date is: ' +data.tomorrow);
                        }
                    });
                });
            });
        </script>
    </body>
    </html>

Step 2: Make controller file for current date and time of any country name.
app/Http/Controllers/DateController.php
public function caldate(Request $request)
{
    $value['yesterday'] = date('d/m/Y',strtotime("-1 days"));
    $value['today'] = date('d/m/Y');
    $value['tomorrow'] = date("d/m/Y", strtotime("+1 day"));
    return response()->json($value);
}

Step 3: Make a route for the connection between controller and view.
routes/web.php
Route::post('caldate', 'DateController@caldate');

Related Post