0

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!

Community
  • 1
  • 1
Gisto
  • 887
  • 2
  • 16
  • 32
  • 1
    This is only your form and the date is read only, you need to show your code that sets/updates the "timestamp" field. – Craig Aug 28 '12 at 21:30
  • Done - I'm just cycling through the fields so there's not much to show. – Gisto Aug 29 '12 at 00:13
  • That's just the display code, you question is about saving the value what about your the code in your model? – Craig Aug 29 '12 at 03:52
  • That's all I'm using. The first one (datepassed) saves the date just fine and is stored in the format of `0000-00-00 00:00:00` How do you grab the time too - all mine look like `2012-08-28 00:00:00` The file above says to use "NOW" for the default but not much about time. – Gisto Aug 29 '12 at 04:16
  • I also edited it a bit, hopefully that's clearer. – Gisto Aug 29 '12 at 04:19

2 Answers2

1

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"
 />
Sasi varna kumar
  • 193
  • 1
  • 2
  • 11
  • Any suggestion for joomla 3.2 version for `format="%d-%m-%Y H:i:s"` and it is not working in joomla 3.2.3 Stable – Kathir Mar 14 '14 at 05:37
0

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.

Craig
  • 9,335
  • 2
  • 34
  • 38