views:

35

answers:

1

Hi, I', trying to re-format a date from a table in Codeigniter. The Controller is for a blog. I was succesfull when the date conversion happens in the View. I was hoping to convert the date in the Model to have things in order.

This is the Model:

    class Novedades_model extends Model {

 function getAll() {
  $this->db->order_by('date','desc'); 
  $query = $this->db->get('novedades');

  if($query->num_rows() > 0) {
   foreach ($query->result() as $row) {
    $data[] = $row;
   }
  }
  return $data;
 }
}

This is part of the controller

$this->load->model('novedades_model');
$data['records'] = $this->novedades_model->getAll();

Here's the date conversion as it happens in the View. This is inside the posts loop:

 <?php foreach($records as $row) : ?>

  <?php 
   $fdate = "%d <abbr>%M</abbr> %Y";
   $dateConv = mdate($fdate, mysql_to_unix($row->date));
  ?>

  <div class="article section">
   <span class="date"><?php echo $dateConv ;?></span>

... Keeps going ...

How can I convert the date in the Model? Can I access the date key and refactor it?

A: 

Why you need to format the date in the Model because ultimately you need the formatted date in the View to be shown. However, you can do the same what you are already doing in the View for formatting it:

 function getAll() {
  $this->db->order_by('date','desc'); 
  $query = $this->db->get('novedades');

  if($query->num_rows() > 0) {
   foreach ($query->result() as $row) {
    $data[] = $row;
   }
  }

  foreach($data as $row) :
   $fdate = "%d <abbr>%M</abbr> %Y";
   $dateConv = mdate($fdate, mysql_to_unix($row->date));
   ............

 }
Sarfraz