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

Working with date format in template

Discussion in 'XenForo Development Discussions' started by AndyB, Dec 8, 2013.

  1. AndyB

    AndyB Well-Known Member

    I have an add-on that I'm creating. The add-on shows a calendar list like this:

    pic001.jpg

    As you can see the date format is a bit difficult to read.

    What functions are available in the template system to make the date format easier to read in this situation?

    Here's the template code:

    Code:
    {xen:phrase calendar}
    <br /><br />
    
    <table class="dataTable">
        <tr class="dataRow">
            <th>{xen:phrase date}</th>
            <th>{xen:phrase title}</th>
            <th>{xen:phrase forum}</th>
        </tr>
    
        <xen:foreach loop="$threads" value="$thread">
        <tr class="dataRow">
            <td>{$thread.calendar_date}</td>
            <td><a href="{xen:link 'threads', $thread}" />{$thread.title}</a></td>
            <td>{$thread.nodeTitle}</td>
        </tr>
        </xen:foreach>
    </table>
    
    I would like to be able to show the date with separators like 2013/12/08 or 12/08/2013.

    Thank you.
     
    Last edited: Dec 8, 2013
  2. Chris D

    Chris D XenForo Developer Staff Member

    Are they the thread creation dates or something else?
     
  3. Jeremy

    Jeremy XenForo Moderator Staff Member

    Use the xen:datetime tag with the UNIX timestamp.
     
  4. AndyB

    AndyB Well-Known Member

    Thank you, Chris and Jeremy.

    The date format I chose (year-month-day) is being used to identify a calendar entry. For example a member posts a new thread that is for a party on 12/24/2013. The date is saved in a table called xf_calendar and the event date is saved as 20131224. I suppose I could store the date as a unix timestamp but was hoping to avoid that.

    Here's my model:

    PHP:
    <?php

    class Andy_Calendar_Model_Thread extends XFCP_Andy_Calendar_Model_Thread
    {
        public function 
    getCalendarThreads($yearMonth)
        {        
            return 
    $this->_getDb()->fetchAll('
            SELECT xf_thread.thread_id, xf_thread.title, xf_calendar.calendar_date, xf_node.title AS nodeTitle
            FROM xf_thread
            INNER JOIN xf_calendar ON xf_calendar.thread_id = xf_thread.thread_id
            INNER JOIN xf_node ON xf_node.node_id = xf_thread.node_id
            WHERE xf_thread.calendar = "1"
            AND xf_calendar.calendar_date LIKE "%' 
    $yearMonth '%" 
            ORDER BY xf_calendar.calendar_date ASC, xf_node.title ASC, xf_thread.title ASC
            LIMIT 100'
    );
        }        
    }

    ?>
     
    Last edited: Dec 8, 2013
  5. Jeremy

    Jeremy XenForo Moderator Staff Member

    Storing the date as a UNIX time stamp allows greater flexibility and allows you to use XenForo's built in functionality to change the display.
     
  6. AndyB

    AndyB Well-Known Member

    Isn't there a substr command or similar in templates?
     
  7. Jeremy

    Jeremy XenForo Moderator Staff Member

    Not in templates, no.
     
  8. AndyB

    AndyB Well-Known Member

    The reason I prefer to use the (year-month-day) format over a UNIX time stamp is that when I want to query the calendar, it's much easier to query by year-month-day format.

    Here's the portion of the code that calls the model and returns the $threads variable.

    PHP:
            // get calendar thread_id numbers, title and other data
            
    $threads $this->getModelFromCache('XenForo_Model_Thread')->getCalendarThreads($yearMonth);
       
            
    // prepare $viewParams for template
            
    $viewParams = array(
                
    'threads' => $threads,
            );
       
            
    // send to template for display
            
    return $this->responseView('Andy_Calendar_ViewPublic_Calendar''andy_calendar'$viewParams);
    Perhaps I can rebuild the $threads array and convert the (year-month-day) format to UNIX time stamp format?
     
    Last edited: Dec 8, 2013
  9. AndyB

    AndyB Well-Known Member

    Here's print_r($threads) snippet.

    PHP:
    Array
    (
        [
    0] => Array
            (
                [
    thread_id] => 127846
                
    [title] => 2013 OTHG Tentative Race Schedule
                
    [calendar_date] => 20131201
                
    [nodeTitle] => Dirt Riders
            
    )

        [
    1] => Array
            (
                [
    thread_id] => 136140
                
    [title] => Hollister Hills Sun 1 Dec
                
    [calendar_date] => 20131201
                
    [nodeTitle] => Dirt Riders
            
    )

        [
    2] => Array
            (
                [
    thread_id] => 136159
                
    [title] => Late noticeLoop Dec 1
                
    [calendar_date] => 20131201
                
    [nodeTitle] => Group Rides
            
    )

        [
    3] => Array
            (
                [
    thread_id] => 136110
                
    [title] => Los Altos Festival of Lights Parade 6 pm Sun12/1/13
                
    [calendar_date] => 20131201
                
    [nodeTitle] => Social Events
            
    )
     
    Last edited: Dec 8, 2013
  10. AndyB

    AndyB Well-Known Member

    I assume the best way to rebuild this array would be to use a foreach.

    PHP:
            // add slashes to year-month-day format
            
    foreach ($threads as $k => $v)
            { 

            }
     
  11. Jeremy

    Jeremy XenForo Moderator Staff Member

    $k will never be calendar_date.
     
  12. AndyB

    AndyB Well-Known Member

    Thank you, Jeremy.

    I tried this but failed miserably.

    PHP:
            // add slashes to year-month-day format
            
    foreach ($threads as $k => $v)
            {
                
    $newThreads = array(
                    
    'thread_id' => $k['$v'],
                    
    'title' => $k['$v'],
                    
    'calendar_date' => $k['test'],
                    
    'nodeTitle' => $k['$v'],
                );
            }
    Any help would be greatly appreciated.
     
  13. AndyB

    AndyB Well-Known Member

    Well I got a little further along in rebuilding my $thread array by using this code:

    PHP:
            // add slashes to year-month-day format
            
    foreach ($threads as $k => $v)
            {
                
    $newThreads[] = array(
                    
    'thread_id' => $k,
                    
    'title' => $k,
                    
    'calendar_date' => $k,
                    
    'nodeTitle' => $k,
                );
            }
    This results in the following output:

    pic001.jpg
     
  14. AndyB

    AndyB Well-Known Member

    I got a little further with this code:

    PHP:
            // add slashes to year-month-day format
            
    foreach ($threads as $k)
            {
                foreach (
    $k as $v)
                {
                    
    $newThreads[] = array(
                        
    'thread_id' => $v,
                        
    'title' => $v,
                        
    'calendar_date' => $v,
                        
    'nodeTitle' => $v,
                    );
                }
            }
    This results in the following output:

    pic001.jpg
     
  15. AndyB

    AndyB Well-Known Member

    The print_r($newThreads)

    Code:
    Array
    (
        [0] => Array
            (
                [thread_id] => 127846
                [title] => 127846
                [calendar_date] => 127846
                [nodeTitle] => 127846
            )
    
        [1] => Array
            (
                [thread_id] => 2013 OTHG Tentative Race Schedule
                [title] => 2013 OTHG Tentative Race Schedule
                [calendar_date] => 2013 OTHG Tentative Race Schedule
                [nodeTitle] => 2013 OTHG Tentative Race Schedule
            )
    
        [2] => Array
            (
                [thread_id] => 20131201
                [title] => 20131201
                [calendar_date] => 20131201
                [nodeTitle] => 20131201
            )
    
        [3] => Array
            (
                [thread_id] => Dirt Riders
                [title] => Dirt Riders
                [calendar_date] => Dirt Riders
                [nodeTitle] => Dirt Riders
            )
    
     
  16. AndyB

    AndyB Well-Known Member

    Finally. Now I'm able to rebuild the $threads array with this code:

    PHP:
            foreach ($threads as $k => $v)
            {
                
    $newThreads[] = array(
                    
    'thread_id' => $v['thread_id'],
                    
    'title' => $v['title'],
                    
    'calendar_date' => $v['calendar_date'],
                    
    'nodeTitle' => $v['nodeTitle'],
                );
            }
    Now I need to figure out how to convert $v['calendar_date'] to UNIX time stamp.
     
  17. Jeremy

    Jeremy XenForo Moderator Staff Member

    You've done nothing but copy the array. You should be storing the date as a unix time stamp, its a universal standard.
     
  18. AndyB

    AndyB Well-Known Member

    Hi Jeremy,

    As I indicated earlier, storing the date in year_month_day format allows for super easy queries of a date range.

    Using this code:

    PHP:
            // convert year_month_day format to UNIX time stamp
            
    foreach ($threads as $k => $v)
            {
                
    $newThreads[] = array(
                    
    'thread_id' => $v['thread_id'],
                    
    'title' => $v['title'],
                    
    'calendar_date' => strtotime($v['calendar_date']),
                    
    'nodeTitle' => $v['nodeTitle'],
                );
            }
    I'm able to convert the year_month_day format to UNIX time stamp so the template can output the date column using the available XenForo code.

    Here is the calender list now:

    pic001.jpg
     
  19. AndyB

    AndyB Well-Known Member

    Now I just need to add the code for the timezone offset.
     
  20. AndyB

    AndyB Well-Known Member

    Timezone offset added.

    PHP:
            // convert year_month_day format to UNIX time stamp
            
    foreach ($threads as $k => $v)
            {
                
    $newThreads[] = array(
                    
    'thread_id' => $v['thread_id'],
                    
    'title' => $v['title'],
                    
    'calendar_date' => strtotime($v['calendar_date']) - XenForo_Locale::getTimeZoneOffset(),
                    
    'nodeTitle' => $v['nodeTitle'],
                );
            }
     

Share This Page