Welcome Guest, Not a member yet? Register   Sign In
Confused about formatting date
#1

[eluser]kaos78414[/eluser]
I want to pull a date from mysql records for blogs and display it like: Day, Month, Year (ex. 11 June, 2010)

But I don't really understand the user guide's examples in the date helper. When I get the date from the db it looks like: 2010-06-11 06:18:15

How can I format that to look readable?

Alternatively, is it possible to pull the day, year and month separately so I can style each in its own div?
#2

[eluser]Burak Guzel[/eluser]
First convert the mysql date to unix time using strtotime(). Then pass it to the date() function to display it in the format you would like.

And yeah, with the date() function, you can display year, month, day separately if you would like.

(These are standard PHP functions, not CI).
#3

[eluser]kaos78414[/eluser]
Perfect. Just for anyone's future reference, here's the code I ended up using to get everything separate. Not sure if there's a better way to do this but in the model I grabbed the date and formatted it:

Code:
function format_blog_date($blog_id) {
        $this->db->where('blogid', $blog_id);
        $q = $this->db->get('blogposts');
        
        if ($q->num_rows() > 0) {
            $date =  $q->row('blogdate');
        }
        
        $data = strtotime($date);
        
        return $data;
    }

And then in the controller I separated the values:

Code:
$daystring = "%j";
        $data['day'] = mdate($daystring, $date);
        $monthstring = "%F";
        $data['month'] = mdate($monthstring, $date);
        $yearstring = "%y";
        $data['year'] = mdate($yearstring, $date);

And passed the data to the view of course, and it's working perfectly! Big Grin
#4

[eluser]mddd[/eluser]
Why not get the correct date straight from Mysql? Mysql's Date_format function does the same thing and you can skip all the steps in between: get the date straight from Myqsl in the correct format.
#5

[eluser]kaos78414[/eluser]
Well if I do it that way can I still separate the values for day, month and year? I'm placing each in different spans for styling purposes. This way serves my purpose fine, but if there is a better/ more efficient way I'd do that. I'm not familiar with date_format, maybe you could give an example?
#6

[eluser]umefarooq[/eluser]
Hi date_format is really easy to use it is like PHP date() function it will give you desired results if you day, month and year separate it can do so also and data processing will really speed up everything happen at database end, here is simple example, you have date 20/06/2010 and you want month out of it which 06 or June

Code:
date_format('20/06/2010','%M') will give you June

or you want output June-20 2010
date_format('20/06/2010','%M-%d %Y')

for more information about date_format check mysql documentation
date_format()




Theme © iAndrew 2016 - Forum software by © MyBB