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

Deekshith's avatar

Accessor change date format not working on ajax post

Hello, I am trying to display 24 hour format time in AM and PM format and i have written accessor like below,

i have a table data slots like below,

09:00:00 - 11:00:00
11:00:00 - 13:00:00
13:00:00 - 15:00:00
15:00:00 - 17:00:00

and Slot.php is,

protected $table = "slots";
    protected $appends = [
        'from_time','to_time'
    ];

    public function getFromTimeAttribute($value)
    {
        return date('h:i A', strtotime($value));
    }

    public function getToTimeAttribute($value)
    {
        return date('h:i A', strtotime($value));
    }

and it is displaying fine in below html ,

<div class="chk-bill mt-20">
            <div class="col-md-6">
            	<div class="form-group">
            		<label>Delivery Date*</label>
            		<input type="text" name="delivery_date" autocomplete="nope" id="delivery_date" value="{{ $setDate }}" class="form-control" required>
            	</div>
            </div>
            <div class="col-md-6">
            	<div class="form-group">
            		<label>Select Slot*</label>
                <select name="slottime" id="slottime" class="form-control" required>
                @foreach($getCurrentSlots as $slot)
            		<option value="{{ $slot->id }}">{{ $slot->from_time }} - {{ $slot->to_time }}</option>
                @endforeach
              </select>
            	</div>
            </div>
            <div class="clearfix"></div>
          </div>

i have written on change date code where it will display the slots based on onchange date like below,

  // $('#delivery_date').datepicker();
  $('#delivery_date').datepicker({
    minDate: 0, // your min date
    maxDate: '+1w', // one week will always be 5 business day - not sure if you are including current day
    // beforeShowDay: $.datepicker.noWeekends // disable weekends
    dateFormat: 'dd-mm-yy',
    onSelect: function(date, instance) {
      var fd = new FormData();
      fd.append( '_token', "{{ csrf_token() }}" );
      fd.append( 'getdate', date );

        $.ajax({
            type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
            url         : "{{url('get-slots-by-date')}}",
  					data        : fd,
            dataType    : 'json', // what type of data do we expect back from the server
            encode      : true,
            processData	: false,
            contentType	: false
        })
  			.done(function(data) {
          console.log(data);
          var appendhtml = '';
          $.each(data, function (key, val) {
            appendhtml += '<option value="'+val.id+'">'+val.from_time+' - '+val.to_time+'</option>';
  			    });

          $('#slottime')
          .empty()
          .append(appendhtml);
        });
  			return false;
     }
});

and controller code is,

$date = $request->getdate;
      $currentDate = date('d-m-Y');
      if($date == $currentDate)
      {
        $currentTime = date('H:i:s');
        $getCurrentSlots = Slots::where('from_time','>',$currentTime)->where('active_status',1)->get();

      } else {
        $getCurrentSlots = Slots::where('active_status',1)->get();
      }

      return $getCurrentSlots;

But this returning the response like below,

[{"id":1,"from_time":"12:00 AM","to_time":"12:00 AM","active_status":1,"created_at":"2020-12-15 00:08:52","updated_at":"2020-12-15 00:08:52"},{"id":2,"from_time":"12:00 AM","to_time":"12:00 AM","active_status":1,"created_at":"2020-12-15 00:08:52","updated_at":"2020-12-15 00:08:52"},{"id":3,"from_time":"12:00 AM","to_time":"12:00 AM","active_status":1,"created_at":"2020-12-15 00:09:30","updated_at":"2020-12-15 00:09:30"},{"id":4,"from_time":"12:00 AM","to_time":"12:00 AM","active_status":1,"created_at":"2020-12-15 00:33:44","updated_at":"2020-12-15 00:09:30"},{"id":6,"from_time":"12:00 AM","to_time":"12:00 AM","active_status":1,"created_at":"2020-12-15 00:33:48","updated_at":"2020-12-15 00:10:43"},{"id":7,"from_time":"12:00 AM","to_time":"12:00 AM","active_status":1,"created_at":"2020-12-15 00:33:51","updated_at":"2020-12-15 00:11:15"}]

it is displaying 12 AM for all from and to time.

0 likes
0 replies

Please or to participate in this conversation.