Date/Time custom format function not working

wallaby9

Member
Hi again (sorry!)

I've created a custom function in custom_functions.php as follows:

function dadabik_bdge_date_format($the_date) { return date_format($the_date, DATE_ISO8601); }

It appears in the drop-down list of custom functions for the field definition, specifically the "Custom formatting function" drop-down, which now looks like this:

1654445814178.png

However, this doesn't seem to be working, as the old date format is still used:

1654445898819.png

I've tried just returning a simple string instead, and that also doesn't work.

The function does not work in either Detail or Edit views.





Your current DaDaBIK version​

You are using DaDaBIK version 11.5-Elba enterprise, installed on 05-21-2022 (installation code: 179456288336fd5460), the latest version of DaDaBIK is 11.5-Elba released on 04-28-2022
You are running the latest release of DaDaBIK

System info​

PHP Version: 7.4.29
mysql version: 5.5.5-10.3.34-MariaDB-0ubuntu0.20.04.1
Web server: LiteSpeed
Client: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36
 

wallaby9

Member
More info:

In the Details view, the Created field data is absent completely:

1654446321316.png

Whilst in the Edit screen, it is present but still displayed in the default format:

1654446391246.png

Clearly the function is failing and the failure is having a different effect for each instance, but how to fix it?
 

eugenio

Administrator
Staff member
Hello,
the custom formatting functions don't work on the edit form, from the in-line documentation:
Specify here the name of the function (if any) to be used to format this field's data in the datagrid view and details view. This function override the built-in formatting functions executed according to the content type (if any).

About the details page, are you sure you are not returning an empty string with your function?

Best,
 

wallaby9

Member
About the details page, are you sure you are not returning an empty string with your function?

I absolutely was, of course, and the reason was that I didn't convert the input string into a date_time object:

Code:
function dadabik_bdge_date_format($the_date) {
    // FAILS (silently) because $the_date is a string, not a date object
    return date_format($the_date, DATE_ISO8601);
}

So I guess this was erroring silently and returning the empty string.

This is the correct way to do it:

Code:
function dadabik_bdge_date_format($the_date) {
    // Convert $the_date to a date object before trying to format it:
    return date_format(date_create($the_date), DATE_ISO8601);
}

It's a shame it doesn't work on the Edit fields but I get why. There must be a way to make that happen, further back at the database level perhaps. Not something I've had to try and do before.

Thanks again 👍
 

eugenio

Administrator
Staff member
If you need to format a date in edit mode, check these config pagameters:
$date_format_edit;
$date_time_format_edit;

If you just need a particular date format for results and details, you could also edit this function format_date in /include/general_functions.php.
The file, however, is overwritten after a DaDaBIK upgrade so you would need to re-apply your changes.

best,
 

wallaby9

Member
FYI whatever function is using these strings in Dadabik, doesn't seem to respect the "e" parameter for the timezone.

This returns a correct datetime string:

$date_time_format_edit = 'Y-m-d H:i:s';

2022-06-01 14:54:10

whereas if I add the "e" to include the timezone like this:

$date_time_format_edit = 'Y-m-d H:i:s e';

it returns the same string but with a literal "e" at the end:

2022-06-01 14:54:10 e

when it should return this:

2022-06-01 14:54:10 BST

Can you think why this wouldn't be working?
 

eugenio

Administrator
Staff member
FYI whatever function is using these strings in Dadabik, doesn't seem to respect the "e" parameter for the timezone.

This returns a correct datetime string:
As stated in the config file, the formats available are described here:
 

wallaby9

Member
Thanks again.

That's an odd decision, that they would choose not to implement that in flatpickr, it seems a pretty standard wish.
 
Top