php - I want to fetch data from datetime column in this form '2015-jan-1'

317

This is my model

function get_archive_links(){
        //$this->db->select('date');
        $this->db->select("date, date_format(date, 'f jS, Y')");
       // $this->db->group_by('MONTH(date), YEAR(date)');
        $query = $this->db->get('blog');
        $results['chart_data'] = $query->result();
        return $results;

    }

This is my controller

public function linechart(){
    $result = $this->linechart->get_archive_links();
    $data['chart_data'] = $result['chart_data'];
    $this->load->view('linechart', $data);
}

I am getting this

2016-01-01 02:20:36.000000

I need this 2016-jan-1

143

Answer

Solution:

Assuming you are getting date in this variable,$chart_data['date'] than use this:

<?
    $date = date("Y-M-d", strtotime($chart_data['date']));
?>
172

Answer

Solution:

use date function to formate date according to your requirement.

public function linechart(){
    $result = $this->linechart->get_archive_links();
    $data['chart_data'] = date('Y-M-d', strtotime($result['chart_data']));
    $this->load->view('linechart', $data);
}

for more information check this: http://www.w3schools.com/php/func_date_date.asp

601

Answer

Solution:

You can do this in select statement itself like this:-
select DATE_FORMAT(NOW(),'%Y-%b-%d') from test
Refer http://sqlfiddle.com/#!9/6d2e0/4 & this link:-http://www.w3schools.com/sql/func_date_format.asp

People are also looking for solutions to the problem: arrays - How to create a function to return variables in php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.