Saving values in datetime fields

76 views
Skip to first unread message

Manfred Hübner

unread,
Mar 12, 2019, 8:48:14 AM3/12/19
to Joomla Component Builder
Hi,
again I have a question regarding date / datetime fields.

When adding a date field, the format property is set for me to %d-%m-%Y as I am in Germany. Unfortunately, no date will be stored (always 0000-00-00).

I tried the Joomla property translateformat (without the format property). The date will be displayed in edit view correctly, but again nothing is saved to the database.

When not using format or translateformat and using the english date format (Y-m-d), everything works.

So I guess that something is going wrong with the format conversion. Does anybody has an idea? Is it bug to open?

Regards
Manfred

TLWebdesignNL

unread,
Mar 12, 2019, 8:50:28 AM3/12/19
to Joomla Component Builder
Hi,

Here is more info on how to set it up properly for us europeans :)



Op dinsdag 12 maart 2019 13:48:14 UTC+1 schreef Manfred Hübner:

Manfred Hübner

unread,
Mar 12, 2019, 8:57:35 AM3/12/19
to Joomla Component Builder
You are awesome!!!

Indeed, setting user_utc in the filter property helps. But the value is stored internally as GMT and displayed in the admin list as "stored". Do you know how to convert it back to the users timezone when displaying? Is there are "magic" switch or do I need to program anything?

Thanks again :-)

TLWebdesignNL

unread,
Mar 12, 2019, 9:04:23 AM3/12/19
to Joomla Component Builder
Well it was not me but René that did all the work figuring out what worked best :)


Op dinsdag 12 maart 2019 13:57:35 UTC+1 schreef Manfred Hübner:

René Kreijveld

unread,
Mar 12, 2019, 9:13:13 AM3/12/19
to Joomla Component Builder
Thanks Tom! ;-)

Op dinsdag 12 maart 2019 14:04:23 UTC+1 schreef TLWebdesignNL:

Manfred Hübner

unread,
Mar 12, 2019, 9:20:46 AM3/12/19
to Joomla Component Builder
Thanks René :-)

Manfred Hübner

unread,
Mar 12, 2019, 9:35:32 AM3/12/19
to Joomla Component Builder
That brings me to the next problem...

Where can I add code to format the output of a field? I know I can do it in the model of the admin view, but I am not sure if I bring confusion to the system when I change the $items->field value (maybe other parts of JCB rely on that). 

Is there a way to have a formatted output that not conflict with the stored value? or do I overcomplicate this issue? 

Thanks again for your help!

TLWebdesignNL

unread,
Mar 12, 2019, 9:47:08 AM3/12/19
to Joomla Component Builder
Where would you want to format this? If i was using this in a field in the front-end i would just use the JDate to format the field value as i would want it and use the user timezone directly inside the view or template/layout file. If i want to use it in multiple places i would create a helper function for it.

Op dinsdag 12 maart 2019 14:35:32 UTC+1 schreef Manfred Hübner:

Manfred Hübner

unread,
Mar 12, 2019, 10:11:01 AM3/12/19
to Joomla Component Builder
I want to format the output in the admin list view.

Because the date is stored in the database as GMT, in admin list view it is also displayed as GMT. I can solve it by injection custom code in the admin list view:

<?php foreach ($this->items as $i => $item): ?>
<!--[INSERTED$$$$]--><!--2-->
<?php
$date = new JDate($item->beratungsdatum);
$tz=JFactory::getUser()->getParam('timezone');
if (!is_null($tz)) {
$timezone = new DateTimeZone(JFactory::getUser()->getParam('timezone'));
$date->setTimezone($timezone);
$item->beratungsdatum = $date->format('Y-m-d H:i:s',true);
}
?>
<!--[/INSERTED$$$$]-->



But it looks for me like firing a bird with a cannon ;-)

Anyone with a better idea?

René Kreijveld

unread,
Mar 12, 2019, 10:27:05 AM3/12/19
to Joomla Component Builder
I solved it in the list view with a small custom PHP script at the PHP getItems Method. I have a field called "gebdat" that holds a birthdate.

foreach ($items as $nr => &$item)
{
 
// convert gebdat
 
if ($item->gebdat == "0000-00-00")
 
{
 $item
->gebdat = "";
 
}
 
else
 
{
 $item
->gebdat = date('d-m-Y', strtotime($item->gebdat));
 
}
}


Op dinsdag 12 maart 2019 15:11:01 UTC+1 schreef Manfred Hübner:

Manfred Hübner

unread,
Mar 12, 2019, 10:57:39 AM3/12/19
to Joomla Component Builder
Hi René.
I transfered my code to the getItems method and it worked :-) Wasn't sure if that has side effects but all seem ok.

regards
Manfred

René Kreijveld

unread,
Mar 12, 2019, 11:08:44 AM3/12/19
to Joomla Component Builder
Op dinsdag 12 maart 2019 15:57:39 UTC+1 schreef Manfred Hübner:
Hi René.
I transfered my code to the getItems method and it worked :-) Wasn't sure if that has side effects but all seem ok.

regards
Manfred

Nice :-) 

TLWebdesignNL

unread,
Mar 12, 2019, 12:37:16 PM3/12/19
to Joomla Component Builder
Manfred you only need:

JHtml::date($item->beratungsdatum, 'D F n, Y H:i:s', true);

No need to fetch user timezone. and all that. just telling JDate that you want to use the user timezone with that "true" parameter does the trick. Tested it just now to make sure.

Kind regards,

Tom

Op dinsdag 12 maart 2019 16:08:44 UTC+1 schreef René Kreijveld:

Manfred Hübner

unread,
Mar 12, 2019, 12:55:25 PM3/12/19
to Joomla Component Builder
What? So easy ...  I have to learn a lot :-D

Thx :-)

Laurent

unread,
Nov 26, 2020, 4:30:59 AM11/26/20
to Joomla Component Builder, Manfred
Hello,

I would like also to format the date in admin list view, where i add this code  JHtml::date($item->beratungsdatum, 'D F n, Y H:i:s', true);  ?

Thanks. 

((ewe))yn

unread,
Dec 19, 2020, 4:47:21 PM12/19/20
to Joomla Component Builder, Laurent, Manfred
We are moving to GitHub Discussions. We will keep this open to give access to old issues, resolved. But please continue conversations on the new github discussions area ;)
Reply all
Reply to author
Forward
0 new messages