• 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

AndyB

Well-known member
#1
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:

AndyB

Well-known member
#4
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:

Jeremy

Well-known member
#5
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.
 

AndyB

Well-known member
#8
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:

AndyB

Well-known member
#9
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 notice: Loop Dec 1
            [calendar_date] => 20131201
            [nodeTitle] => Group Rides
        )

    [3] => Array
        (
            [thread_id] => 136110
            [title] => Los Altos Festival of Lights Parade - 6 pm Sun, 12/1/13
            [calendar_date] => 20131201
            [nodeTitle] => Social Events
        )
 
Last edited:

AndyB

Well-known member
#10
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)
        { 

        }
 

AndyB

Well-known member
#12
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.
 

AndyB

Well-known member
#13
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
 

AndyB

Well-known member
#14
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
 

AndyB

Well-known member
#15
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
        )
 

AndyB

Well-known member
#16
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.
 

Jeremy

Well-known member
#17
You've done nothing but copy the array. You should be storing the date as a unix time stamp, its a universal standard.
 

AndyB

Well-known member
#18
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
 

AndyB

Well-known member
#20
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'],
			);
		}