We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1331
    • 129 Posts
    Using 1.0.2 on a site. (I know I need to update)

    I’m using the datepicker to allow users to select a date. But I also want to use this field as the sortBy in Ditto. However, it is sorting them alphabetically due to the MM dd, YYYY format that I’m outputting the dates on the front end.

    Is there any way to display the date as Jun 3, 2010 and ask Ditto to sort based on another format like YYYY-MM-DD? Or would I just have to create two separate template variables?
      • 1122
      • 209 Posts
      This is solveable through a custom sort -- you need to convert your textual date (created by picker’s widget) back to unix timestamp and then perform a comparison. The following small extender "sortByDatePicker.extender.inc.php" should do this task:
      <?php
      if (!function_exists('sortByDatePicker')) {
          function sortByDatePicker($a_doc, $b_doc) {
              $a_stamp = strtotime($a_doc['date']);
              $b_stamp = strtotime($b_doc['date']);
              return ($a_stamp < $b_stamp ? -1 : ($a_stamp > $b_stamp ? 1 : 0));
          }
      }
      
      $orderBy['custom'][] = array('date', 'sortByDatePicker');
      $ditto->advSort = TRUE;
      ?>
      

      where ’date’ stands for the name of TV holding user-inserted date.

      And finally, Ditto call with custom sort:
      [[Ditto? &parents=`...` &display=`...` &tpl=`...` &extenders=`sortByDatePicker`]]