开发者

Formating a date field in the Model (Codeigniter)

开发者 https://www.devze.com 2023-01-03 19:31 出处:网络
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

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 开发者_如何学Gothe 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?


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));
   ............

 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号