in my custom component I have a calendar field which I render in my view.

Database field :

Name: estEventDate Type: date Null: No Default: 0000-00-00

XML Field :

<field name="estEventDate" type="calendar"   
  format="%Y-%m-%d"
  todaybutton="false"
  filltable="false"
  showtime="false"
  size="22"
  class="validate-dates uk-input"
  minyear="0"/>

The php file tmpl/default.php

<div>
  <?php echo $this->form->renderField('estEventDate'); ?>
</div>

The erros is : 0 DateTime::__construct(): Failed to parse time string (-1-11-30) at position 5 (-): Double timezone specification

And in the log I get this :

error   Uncaught \Throwable of type Exception thrown. Stack trace: 
#0 /home/mike/joomla/libraries/src/Date/Date.php(112): DateTime->__construct('-1-11-30', Object(DateTimeZone))
#1 /home/mike/joomla/libraries/src/Factory.php(548): Joomla\CMS\Date\Date->__construct('-1-11-30', Object(DateTimeZone))
#2 /home/mike/joomla/libraries/joomla/form/fields/calendar.php(238): Joomla\CMS\Factory::getDate('-1-11-30', 'UTC')
#3 /home/mike/joomla/libraries/src/Form/FormField.php(975): JFormFieldCalendar->getInput()
#4 /home/mike/joomla/libraries/src/Form/Form.php(670): Joomla\CMS\Form\FormField->renderField(Array)
#5 /home/mike/joomla/components/com_contract/views/contract/tmpl/edit_admin.php(95): Joomla\CMS\Form\Form->renderField('estEventDate')

I have tried different options in the xml. (like format,translateformat etc) I also have set a date directly in the database (I thought that mayby '0000-00-00' value was the problem)

Joomla v. 3.9.0 / php 7.2

up vote 2 down vote accepted

My suggestion would be, simply change the database field to varchar or text for saving this form field and then the error should be gone. Then you can get back the string format date values from the database when you need them and you can create any date/datetime format output from the string date that you get from the database. I suggest this, since with the form field rendering does not have any problem basically as I see. And I also dare to suggest this since at plugin params (in extensions table) usually the date fields are also saved in text database fields and that's not a big problem in Joomla. And finally this way you can also spot out in the database date string if the saved date format is not a valid date format. Thus you can repair.

Or, staying at date or datetime field in database, from the xml field definition I would delete the format="%Y-%m-%d" attribute definition, since that might also cause this error (I'll will also check this in my forms).

A SHORT UPDATE TOWARD FINDING THE REAL CAUSE OF THIS PROBLEM (just for later researchers)

@mixahlos found the final solution for his above detailed problem, however for me it still remained a bit unclear what caused the wrong date format in the first place. Some important points summarized and a kind of check list on this question/problem:

1. The date field is actually a simple text field at input with a calendar input helper.

2. The same problem appeared in K2 component a bit more than a year ago, which was caused by an outdated javascript calendar input code used in K2. That caused conflict at the date input. In this particular case this was not the problem.

3. @mixahlos traced back the problem with creating the same Form with the same date Fields in a clear Joomla install where he spotted the basic date formatting problem when he tried to get back the date field data from the database. Then in his code he unset the unneeded data in his PHP code (in a custom Joomla controller) and that solved the initial problem. Thus he can now save the dates without problem into date type database fields.

I hope this can help a bit people later with similar problems. At least with few approaches on handling this problem.

  • 1
    While this may solve the issue, I find it to be suboptimal advice because saving date data as anything other than date type in the database is not best practice. It would be better to solve the issue than to work around it. – mickmackusa Dec 1 at 11:03
  • 1
    Well before I see your post, I changed the database field from "date" to "datetime". I didn't change the anything else in my files -except some if ( value = '0000-00-00') statements- And it worked. I guess the calendar field needs datetime even if no time is used. – mixahlos Dec 1 at 11:30
  • @mickmackusa - yes, you are right, that is definitely not an optimal solution if anyone is saving date as text (string), except if the date format is absolutely sure that it is in valid date or datetime format. I agree on this. – Zollie Dec 1 at 11:52
  • @mixahlos - at least the problem is solved now :) (you can still check that further though one time, since it should work with date type database fields too). – Zollie Dec 1 at 11:54
  • @Zollie I did changed it back and I noticed that when db field type is date, then the rendered field instead of being empty it displays "-1-11-30" which when page is loaded is replaced with "2018-01-11". Inspecting the element I see the following attributes in the element : value="-1-11-30" data-local-value="2018-01-11", When field is DateTime the field is blank and in the element attribute the "value" is empty and there is an attribute data-alt-value="0000-00-00 00:00:00". I am not sure if makes any difference but I have 3 more Calendar fields in the form of type "DateTime" – mixahlos Dec 1 at 17:21

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.