Use Carbon class
php dataTables date format
Greetings Pros,
Can someone help me format the date from dataTables? I tried this but didn't work.
{{ date("Y-m-d H:i:s", strtotime(' + data["full"] + ')) }}
Thanks!
Not sure what + data["full"] + is from but
Non Carbon way
{{ date("Y-m-d H:i:s", strtotime($data['full'])) }}
sorry it's
{{ date("Y-m-d H:i:s", strtotime($data['created_at'])) }}
And here's the whole code:
$.getJSON(" {{ URL::to('/') }}/getHistory", function(data) {
$('#history').dataTable({
"aaData": data,
"aaSorting": [[ 5, 'desc' ]],
"oLanguage": {
"sLengthMenu": "No. of Items to display _MENU_",
"oPaginate": {
"sFirst": "↞ First", // This is the link to the first
"sPrevious": "← Previous", // This is the link to the previous
"sNext": "Next →", // This is the link to the next
"sLast": "Last ↠" // This is the link to the last
}
},
//DISPLAYS THE VALUE
//sTITLE - HEADER
//MDATAPROP - TBODY
"aoColumns":
[
{"sTitle": "#", "sWidth": "70px", "mDataProp": "id", "sClass": "size-14"},
{"sTitle": "User", "sWidth": "70px", "mDataProp": "user"},
{"sTitle": "Event", "sWidth": "100px", "mDataProp": "event"},
{"sTitle": "Field", "sWidth": "200px", "mDataProp": "field"},
{"sTitle": "Object", "mDataProp": "object"},
{"sTitle": "Created at", "sWidth": "100px", "mDataProp": "created_at"}
],
"aoColumnDefs":
[
//FORMAT THE VALUES THAT IS DISPLAYED ON mDataProp
//ID
{ "bSortable": false, "aTargets": [ 0 ] },
{
"aTargets": [ 0 ], // Column to target
"mRender": function ( data, type, full ) {
// 'full' is the row's data object, and 'data' is this column's data
// e.g. 'full[0]' is the comic id, and 'data' is the comic title
return '<label class="text-center size-14">' + data + '</label>';
}
},
//APPLICANTS FULLNAME
{
"aTargets": [ 1 ], // Column to target
"mRender": function ( data, type, full ) {
// 'full' is the row's data object, and 'data' is this column's data
// e.g. 'full[0]' is the comic id, and 'data' is the comic title
return '<label class="size-14 dtem">' + full["user"] + '</label>';
}
},
{
"aTargets": [ 2 ], // Column to target
"mRender": function ( data, type, full ) {
// 'full' is the row's data object, and 'data' is this column's data
// e.g. 'full[0]' is the comic id, and 'data' is the comic title
return '<label class="size-14 dtem">' + full["event"] + '</label>';
}
},
//Field
{
"aTargets": [ 3 ], // Column to target
"mRender": function ( data, type, full ) {
// 'full' is the row's data object, and 'data' is this column's data
// e.g. 'full[0]' is the comic id, and 'data' is the comic title
return '<label class="text-center size-14">' + full["field"] + '</label>';
}
},
{
"aTargets": [ 4 ], // Column to target
"mRender": function ( data, type, full ) {
// 'full' is the row's data object, and 'data' is this column's data
// e.g. 'full[0]' is the comic id, and 'data' is the comic title
return '<label class="text-center size-14"> ' + full["object"] + ' </label>';
}
},
{
"aTargets": [ 5 ], // Column to target
"mRender": function ( data, type, full ) {
// 'full' is the row's data object, and 'data' is this column's data
// e.g. 'full[0]' is the comic id, and 'data' is the comic title
return '<label class="size-14">{{ date("Y-m-d H:i:s", strtotime(' + data["created_at"] + ')) }}</label>';
}
}
],
"fnDrawCallback": function( oSettings ) {
/* Need to redo the counters if filtered or sorted */
if ( oSettings.bSorted || oSettings.bFiltered )
{
for ( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ )
{
$('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[i] ].nTr ).html( "<label>" + (i+1) + "</label>" );
}
}
}
});
$('div.dataTables_filter input').attr('placeholder', 'Category / Date Created...');
});
$data->created_at->toDateTimeString();
It's a javascript code and the data im getting there is in JSON form.
It always return this date format
1970-01-01 08:00:00
all the date is the same.
anyone?
It seems like you are trying to execute PHP (Blade) code within Javascript....
return '<label class="size-14">{{ date("Y-m-d H:i:s", strtotime(' + data["created_at"] + ')) }}</label>';
In this code, data['created_at] is a variable available in your Javascript, but Blade has no access to this variable, so you expect:
{{ date("Y-m-d H:i:s", strtotime('2014-12-28 04:24:00')) }}
to be executed, in reality this is executed:
{{ date("Y-m-d H:i:s", strtotime()) }}
That explains why you always have this date: 1970-01-01 08:00:00
You should transform the date when you are creating the JSON on the server side.
Thanks for the hint!
Please or to participate in this conversation.