PRINT date format not consistent

Enterprise v3.1

The date format of a column in a datagrid is correct on the screen dd-mm-yyyy.
But if I try to print that datagrid from the GroceryCrud print button, the output format changes to yyyy-mm-dd.

I have tried:
$crud->fieldTypeColumn(‘DOB’, ‘native_date’);
$crud->fieldType(‘DOB’, ‘native_date’);

But nothing seems to fix the issue.
Any suggestions?

Ray

Hello @mopac ,

It seems that this is a bug and I can reproduce it as well. I will try to have it fixed for the newer version. If I see that the new release is taking longer (preparing 3.3 which is a bigger one) I will try to suggest you a work-around for this.

Regards
Johnny

Hi Johnny,
Thanks for your support
Ray

Hello Ray,

For now, here’s a workaround that keeps both outputs consistent. Add a callbackColumn on the date field:

$crud->callbackColumn('my_date', function ($myDate) {
    return $myDate ? date('d/m/Y', strtotime($myDate)) : '';
});

If you also have datetime fields, use the same approach with the time included:

$crud->callbackColumn('my_datetime', function ($myDateTime) {
    return $myDateTime ? date('d/m/Y H:i', strtotime($myDateTime)) : '';
});

These run in PHP instead of the browser, so they apply the same way to both the datagrid and the print output, and the mismatch goes away.

Why it’s tricky: the datagrid gets its date format from the browser’s locale, which usually just works. But the print/PDF/Excel output is generated server-side, and PHP has no way to know what format the browser used. A real fix means bringing back a date_format-style setting that was removed in v3.

I’ve been looking into this, and I might get it into an upcoming version — no promises yet, since it’s a bigger change. Hoping for 3.3.0, though 3.3.1 is more likely, since 3.3.0 is already delayed by a large frontend refactor that I am working now.

Let me know if the work-around worked for you :slight_smile:

Regards
Johnny

Hi Johnny,

Thanks for the fast response.
The workaround works well.
Now the print format is the same as the display format.
And the sorting of those date columns still works.
The only hiccup is the quick search function throws an error. I think because a string is being search for in the database column that holds dates.
This for me is not a big issue, but for the release solution, the search function should work.
Thanks again

Ray

Hello @mopac , I can’t reproduce the issue that you are referring to. What database are you using :thinking: ? A quick solution into that is to use the function fieldTypeSearchColumn for example in your case this can be:

$crud->fieldTypeSearchColumn('my_date', 'date');

Let me know if that worked for you.