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

Charitha's avatar

Change jQuery input date to SQLdate to be used in Ajax call

Im struggling to match the date value in the MySQL server with the jquery UI input date

This is what i have tried

$('#start_date').datepicker({dateFormat:'dd-mm-yy'});

ajax call

$(document).on('change', '#hotelid', function() {
    
      var hotid = $('#hotelid').val();
      
      onHotelIdUpdate(hotid);
    });
    function onHotelIdUpdate(val) {
         var bookdate = $('#start_date').val();
         
    console.log('onHotelIdUpdate() called with the val: ', val);
         $.ajax({
         type:'get',
         url: "{{ route('hotel.rate') }}",
         data:{     
               '_token':$('input[name=_token]').val(),
               'hotelid': val, 
                'bookdate': bookdate            
               }, 
               
             success:function(data){
            console.log(data);
            $("#avail").val(data.rooms);
                },
            error: function(){
           console.log("Error Occurred");
                 }

my controller function in laravel


public function getRates(Request $request )
    {
           $hotelid = $request->input('hotelid');
           $date = $request->input('bookdate');
           $rates=hotelroom::where('hotel_id',$hotelid)
                       ->where('available_date',$date)
                       ->first();                 
                           
           return response()->json($rates);
      
     }

This works fine without the "bookdate" variable im sending but when i include it returns blank record i have also notices in the input checkbox the date is showing as " dd-mm-yyyy' but in the database the date value im trying to match has the date as " yyyy-mm-dd" how to fix this ?

0 likes
7 replies
MLCochrane's avatar

Hey @charitha,

It looks like there's a utility function on the datepicker widget for formatting the date: $.datepicker.formatDate( format, date, options )

More details here: https://api.jqueryui.com/datepicker/ just after the quick nav and keyboard interactions.

If you're still having issues with the widget you could also try hardcoding a date into your request to confirm that it is simply the formatting of the date that is the culprit.

1 like
Charitha's avatar

Thanks !,i tried the hard-coding date and it works , so its an issue with a date conversion , just need to convert var bookdate = $('#start_date').val(); to the format yyyy-mm-dd

Cronix's avatar

Another option is to just use carbon in your controller, and convert the date to yyyy-mm-dd format before saving in the db.

1 like
Charitha's avatar

to answer your first comment i tried yy-mm-dd too (earlier ) but it remains the same

Cronix's avatar
Cronix
Best Answer
Level 67

Then just try converting it when saving.

YourModel::create([
    'start_date' => Carbon\Carbon::parse($request->start_date, 'd-m-y')->format('Y-m-d'),
    // other fields...
]);

You might have to play with the d-m-y part, but I think that would be right coming from javascript dd-mm-yy format.

Please or to participate in this conversation.