Welcome Guest, Not a member yet? Register   Sign In
Question about the template parser
#1

[eluser]victorche[/eluser]
It is a newbie question and it is maybe more php than CI specific...
I am developing a really small and light blog application with only a few features. CI is the best for my needs.
Anyway I am using the template parser class as I prefer not to have php tags in my html.
And because my blog entries are coming from the database, in my controller I am using:
Code:
'blog_posts' => $query->result_array()
This is giving me the title, the body and the published date. So far, so good ... But my published date is timestamp so I want to do some actions about these results. I want to have a more human look of the post date. I may even use some nice dates, as described here:

http://ellislab.com/forums/viewthread/106557/

So with a few words, result_array() is giving me:
Code:
1278746700
I want it to be for example:
Code:
Saturday, July 10th 2010, 09:25:00

Please, help me and sorry for this newbie question...
#2

[eluser]markup2go[/eluser]
You could use PHP date()

Code:
<?php
// In a view
foreach($blog_posts as $post)
{
    echo date("n/j/Y g:ia", $post['timestamp']);
}
?>


or MySQL DATE_FORMAT()

Code:
<?php
// In a model
function get_posts()
{
    $this->db->select('blog_posts.*, DATE_FORMAT('blog_posts.timestamp', '%e/%c/%Y %l:%i%p') as formatted_date');
    // etc
}
?>
#3

[eluser]victorche[/eluser]
Thanks, markup2go ...
Anyway the first option is not for me ... I am using the template parser, so in my views I have:
Code:
{post_entries}
<h3>{post_date}: {post_title}</h3>
<p>{post_body}</p>
{/post_entries}
And in my controller I have:
Code:
$query = $this->db->query(SELECT * FROM posts);

$data = array(
              'post_entries' => $query->result_array()
            );

$this->parser->parse('blog_tpl', $data);
So, you're right... maybe I have to make it in the query I think. Is this the right solution or not?




Theme © iAndrew 2016 - Forum software by © MyBB