Confused about formatting date - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Confused about formatting date (/showthread.php?tid=31265) |
Confused about formatting date - El Forum - 06-11-2010 [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? Confused about formatting date - El Forum - 06-11-2010 [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). Confused about formatting date - El Forum - 06-11-2010 [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) { And then in the controller I separated the values: Code: $daystring = "%j"; And passed the data to the view of course, and it's working perfectly! Confused about formatting date - El Forum - 06-14-2010 [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. Confused about formatting date - El Forum - 06-14-2010 [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? Confused about formatting date - El Forum - 06-14-2010 [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 for more information about date_format check mysql documentation date_format() |