• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Datepicker not updating value when existing value is set

#1
I'm creating a little admin section that is a text box and a date picker. Here is the field for the date picker:

HTML:
    <dl class="ctrlUnit">
        <dt><label for="ctrl_effectiveDate">Effective Date:</label><dt>
        <dd>
            <input type="date" name="effectiveDate" value="{$item.effectiveDate}" class="textCtrl" id="ctrl_effectiveDate" />
        </dd>
    </dl>
When I include {$item.effectiveDate} the date picker defaults to today and no matter what I change it to it will not update when I save because the value attribute will retain the timestamp (value="1411686000"). I noticed in other places in XenForo where the date picker is used the value will become blank but for some reason mine doesn't follow that behavior.

If I do not include {$item.effectiveDate} in the value I can update the item but there is no way to show what it is currently set at. The other two fields title and text always seem to update. Any idea what I'm missing?

Code:
CREATE TABLE `item` (
  `item_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(75) NOT NULL DEFAULT '',
  `text` text NOT NULL,
  `effectiveDate` int(10) unsigned NOT NULL,
  PRIMARY KEY (`item_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
 
Last edited:

Jeff Berry

Well-known member
#2
I don't think a timestamp is the proper format for the value.

If you look at other uses of the date picker in XF, you'll notice they convert to a "picker" format first (which is Y-m-d) if I remember correctly.

Try changing your value to this:
Code:
{xen:date {xen:if $item.effectiveDate, $item.effectiveDate, $serverTime}, picker}
 
#3
I ended up translating the timestamp by creating a DateTime object in the controller.
PHP:
$dateTime = new DateTime('@' . $item['effectiveDate']);
$item['effectiveDate'] = $dateTime->format('Y-m-d');
Just a note for anyone playing around with dates. Don't forget your timezone matters!
 

Jeff Berry

Well-known member
#5
I ended up translating the timestamp by creating a DateTime object in the controller.
PHP:
$dateTime = new DateTime('@' . $item['effectiveDate']);
$item['effectiveDate'] = $dateTime->format('Y-m-d');
Just a note for anyone playing around with dates. Don't forget your timezone matters!
Using the built in XenForo functions adjusts to the timezones by default. However your solution, though not ideal, is fine.