0

Hi guys having some trouble here with my date's output currently it reads 2015-05-05 10:05:22 but i would like to have it render as eg. Monday 12 Mei 2013 how would i achieve this in codeigniter?

Here is my code for creating the post: (Controller)

    public function new_post(){
        if($_POST){
            $data = array(
                'title' => $_POST['post_title'] , 
                'post' =>  $_POST['post_entry'],
                'slug' => $_POST['seo_slug'],
                'active' => 1
            );
            $this->db->insert_post($data);
            redirect(base_url() .'home');
        } else {
            $data['main_content'] = 'blog/new_post';
            $this->load->view('templates/2015/template',$data); 
        }
    }

And then the model:

    public function insert_post($data){
        $this->db->insert('blog',$data);
        return $this->db->insert_id();
    }

and the view:

                <div class="panel panel-default" id="blog-roll">
                <div class="panel-body">
                <div id="blog">
                <div id="blog_post">  
                <?php if(count($posts) == 0) { ?>
                <p>There are currently no posts on my blog, please check back later!</p>
                <?php } else {
                    foreach ($posts as $post) {
                    ?>
                    <h2><a href="<?=base_url()?>blogs/post/<?=$post['slug']?>"><?=$post['title']?></a></h2>
                    <p><?=substr(strip_tags($post['post']),0,300)."."?></p>
                    <p id="info-box"><i>
                        Posted by:&nbsp;&nbsp;<i class="fa fa-user"></i> Riaan Venter 
                        on&nbsp;&nbsp;<i class="fa fa-calendar">&nbsp;</i>: <?=$post['date_added']?>
                    </i></p>
                    <hr>
                    <?php
                    }
                }
                ?>
                </div>
                </div> <!-- blog roll -->
            </div>
            </div>

Some help would really be appreciated.

2
Contributors
8
Replies
30
Views
2 Years
Discussion Span
Last Post by Ventech_IT
0

Monday 12 Mei 2013

Is that right? May or is this in a different language to English?

The DateTime object should be able to deal with your needs.

Assuming $post['date_added'] is the data to convert, although you don't mention it specifically.

$dt = new DateTime($post['date_added']);
echo $dt->format('l d F Y');

You may want to substitute d for j if you don't want leading zeroes on the day date.

0

hahaha yeah that was just as an example for output lol sorry and i am Afrikaans so i had a typo there, would i set this in the insert_post function in the controller?

Edited by Ventech_IT: forgot to add line

0

You could add it to the controller, but this is a general function that could bbe used anywhere, so instead of re-creaating this function over and over again in a number of controllers (or models), you could write this to a static method.

For example:

class RiaanDate
{
    public static function format($date, $format='l d F Y')
    {
        $dt = new DateTime($date);
        return $dt->format($format);
    }
}

Depending on how you load your classes (autoload or manual) - sorry been so long since I used CI. E.g.:

require '/libraries/riaandate.class.php';
...
$date = RiaanDate::format($post['date_added']);

From your controller, I can't ake out what is going on. There seems to be a $data variable in the else branch, which cannot exist. I can't see how this is all related to inserting a new post. Could you explain?

0

Okay so if i add a post to my blog, it goes to the new_post function in the controller i use $data as an array to get the information from the form and when the $data variable get's passed to the model and inserted to the table.

The table's date_added field automatically get's the date the post was inserted as the field is a timestamp.

So when i run the for loop in the view i would just like the date to not display as unix timestamp. if that makes sense.

0

COnfused. The var being passed to the view is called $data, but I see no content relating to $posts.

So you could do something like:

array_walk($posts, function(&$post){$post['date_added']= RiaanDate::format($post['date_added'])});

Place that just before you pass the data ($posts ??) to the view.

Edited by diafol

0

Entire Blogs Controller:

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Blogs extends CI_Controller{

    public function __construct(){

        parent::__construct();
        $this->load->model('Blog');
    }

    public function index(){
        $data['posts'] = $this->Blog->get_posts();
        $data['title'] = 'Ventech Computers - Our Blog';
        $data['main_content'] = 'blog/index';
        $this->load->view('templates/2015/template',$data); 
    }

    public function post($postID){
        $data['post'] = $this->Blog->get_post($postID);
        $data['title'] = 'Ventech Computers - Our Blog';
        $data['main_content'] = 'blog/post';
        $this->load->view('templates/2015/template',$data);         
    }

    public function new_post(){
        if($_POST){
            $data = array(
                'title' => $_POST['post_title'] , 
                'post' =>  $_POST['post_entry'],
                'slug' => $_POST['seo_slug'],
                'active' => 1
            );
            $this->db->insert_post($data);
            redirect(base_url() .'home');
        } else {
            $data['main_content'] = 'blog/new_post';
            $this->load->view('templates/2015/template',$data); 
        }
    }

    public function edit_post($postID){
        $data['success'] = 0;
            if($_POST){
                $data_post = array(
                    'title' => $_POST['post_title'],
                    'post' => $_POST['post_entry'],
                    'active' => 1
                );
                $this->Blog->update_post($postID, $data);
                $data['success'] = 1;
            }
            $data['post'] = $this->Blog->get_post($postID);
            $data['main_content'] = 'blog/edit_post';
            $this->load->view('templates/2015/template',$data); 
    }

    function delete_post($postID){
        $this->Blog->delete_post($postID);
        redirect(base_url());
    }
    }

Entire Blog Model:

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Blog extends CI_Model{

    public function get_posts($num=5,$start=0){
        $this->db->select()->from('blog')->where('active', 1)->order_by('date_added','desc')->limit($num,$start);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function get_post($postID){
        $this->db->select()->from('blog')->where(array('active'=>1,'slug'=>$postID))->order_by('date_added','desc');
        $query = $this->db->get();
        return $query->first_row('array');
    }

    public function insert_post($data){
        $this->db->insert('blog',$data);
        return $this->db->insert_id();
    }

    public function update_post($postID,$data){
        $this->where('postID',$postID);
        $this->db->update('blog',$data);
    }

    public function delete_post($postID){
        $this->db->where('postID',$postID);
        $this->db->delete('blog');
    }
}

And the place in the view where the results are retrived:

                <div class="panel panel-default" id="blog-roll">
                <div class="panel-body">
                <div id="blog">
                <div id="blog_post">  
                <?php if(count($posts) == 0) { ?>
                <p>There are currently no posts on my blog, please check back later!</p>
                <?php } else {
                    foreach ($posts as $post) {
                    ?>
                    <h2><a href="<?=base_url()?>blogs/post/<?=$post['slug']?>"><?=$post['title']?></a></h2>
                    <p><?=substr(strip_tags($post['post']),0,300)."."?></p>
                    <p id="info-box"><i>
                        Posted by:&nbsp;&nbsp;<i class="fa fa-user"></i> Riaan Venter 
                        on&nbsp;&nbsp;<i class="fa fa-calendar">&nbsp;</i>: <?=$post['date_added']?>
                    </i></p>
                    <hr>
                    <?php
                    }
                }
                ?>
                </div>
                </div> <!-- blog roll -->
            </div>
            </div>

Edited by Ventech_IT: code

1

OK, so for:

$data['posts'] = $this->Blog->get_posts();

straight after, do:

array_walk($data['posts'], function(&$post){$post['date_added']= RiaanDate::format($post['date_added'])});

That is if you've used a static method, otherwise, you can do the full monty after it:

array_walk($data['posts'], function(&$post)
    {
        $dt = new DateTime($post['date_added']);
        $post['date_added'] = $dt->format('l d F Y');
    });
0

Success! Thank you so much for your time and input really appreciate it.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.