PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, April 18, 2022

[FIXED] How to remove time from date in yajra datatable

 April 18, 2022     jquery, laravel     No comments   

Issue

I am using Laravel yajra datatable and I want to show only date in column , but unfortunately I am also getting time please help me how can I show date ?

enter image description here

start date and and date should be like that in date column.

2022-04-17 - 2022-04-23

BookingController

  public function datatables()
    {

        $booking = Booking::select(['id','start_datetime','end_datetime'])->get();

        return Datatables::of($booking)
        ->addColumn('mergeColumn', function($row){
        return $row->start_datetime.' - '.$row->end_datetime;
        })
        ->make(true);
    }

Jquery Script

 $(document).ready(function() {

    var table = $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ route('datatables.data') }}",
        "columns": [
            {
                "data": "mergeColumn",
                "defaultContent": ""
            },

        ],
        "columnDefs": [{
                "targets": 'no-sort',
                "orderable": false,
            },
              {
            "targets": 0,
            "render": function(data, type, row, meta) {
                    return  data;
            },
        },

        ],
        "drawCallback": function (settings) {
      },
        //scrollX:true,
    });

    });

Solution

You can use the Carbon library in Laravel to parse your datetime strings and format them in any way you like. The controller code would look something something like this:

use Illuminate\Support\Carbon;

// ...

public function datatables()
{
    $booking = Booking::select(['id','start_datetime','end_datetime'])
        ->get()
        ->map(function($booking) {
            $start = Carbon::parse($booking->start_datetime);
            $end = Carbon::parse($booking->end_datetime);
            return [
                'id' => $booking->id,
                'start_date' => $start->format('Y-m-d'),
                'end_date' => $end->format('Y-m-d'),
            ];
        });

    return Datatables::of($booking)
        ->addColumn('mergeColumn', function($row){
            return $row['start_date'] . ' - ' . $row['end_date'];
        })
        ->make(true);
}

You can get the time values from the $start and $end variables too if you need them.



Answered By - Michael Grove
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing