Question

New at Joomla dev, writing a simple backend component where I have two date fields. One is a date the user can choose from a calendar popup (1st one below), the second is a date/time stamp that the user can't change but needs to see what that last timestamp was. In the edit form the first one (datepassed) is working and grabbing today's date. The second (timestamp) only works when the record is created and the time isn't being saved at all. I need it to always grab the present time and date.

From the XML file:

  <field name="datepassed"
    type="calendar"
    label="Date Completed"
    default="NOW"           
  /> 

  <field name="timestamp"
    type="calendar"
    label="Last Modified"
    default="NOW"
    readonly="true" 
    class="readonly"
  />

And in the edit.php view:

  foreach ($this->form->getFieldset('fields_logged') as $field): 
    echo '<li>';
        echo $field->label;
        echo $field->input; 
     echo '</li>';
  endforeach;

Found this link with a similar question, and this one from Joomla docs...not much help in the way of time though.

Any ideas why that timestamp isn't working, or is there a better way of doing this?

Thanks!

Was it helpful?

Solution

OK, from your updated question, it appears there are two simple misunderstandings going on.

The default value is only set if the field value is null so it will work on the first save but subsequently as there is a value it won't be replaced with the current timestamp.

When you use a type of Calendar it will only return the date sans the time component. If you look at the /libraries/joomla/html/parameter/element/calendar.php you will see that it's asking for '%Y-%m-%d'

To update the timestamp you will want to modify your model so that when you get the item it updates the timestamp field with the current date & time e.g. $item->timestamp = JFactory::getDate()

Or possibly update it just in the store() if you need it to be minutes and seconds accurate as conceivable the item could be opened and not saved for several minutes. This is probably the better approach as you want your user to be able to see the last time the item was saved.

OTHER TIPS

There is a more simple way of doing what you want, no need to write override for model, we its another attribute for the calendar type field in joomla, refer http://docs.joomla.org/Calendar_form_field_type

you can use in your code like,

 <field name="datepassed"
type="calendar"
label="Date Completed"
default="NOW"    
 format="%d-%m-%Y H:i:s"
 /> 

<field name="timestamp"
type="calendar"
label="Last Modified"
default="NOW"
readonly="true" 
class="readonly"
format="%d-%m-%Y H:i:s"
 />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top