Im trying to get the mysql timestamp to go to actual time. The PHP code im using though code igniter echos out the pub date for news articles but, its in this format yyyy-mm-dd hh:mm:ss I want it in MM/DD/YY HH:MM the code echos it out after running a query from a controller here is the code from the view <?php echo $row->pubdate; ?>
pubdate is the column name from the MYSQL database. I've looked around stack overflow for an answer to this but I have not been able to find anything that works for my situation.
Asked
Active
Viewed 393 times
0

David
- 389
- 5
- 22
1 Answers
1
You could create a function which takes a MySQL formatted date and returns the format you want, something like this:
function nice_date($mysql_date)
{
list($date,$time) = explode(" ",$mysql_date);
list($year,$month,$day) = explode("-",$date);
return $month.'/'.$day.'/'.$year.' '.$time;
}
Use explode to split the given date into $date
and $time
variables (using list to assign the variables), then split $date
into year, month and day. Then simply return a string with the variables reordered.

GluePear
- 7,244
- 20
- 67
- 120
-
Okay I got all of that, and i belive I loaded my function in my controller by using this `$this->load->niceDate;` I still don't know how to get it into my view thought I tried this `pubdate->niceDate; ?> ` but I got an error Trying to get property of non-object – David Sep 05 '15 at 21:11
-
Where did you put the `nice_date` (sorry, I renamed it!) function? – GluePear Sep 05 '15 at 21:15
-
So in the view, try `echo $this->HomeModel->nice_date($row->pubdate);` – GluePear Sep 05 '15 at 21:17
-
Yep it worked! Thanks for the help – David Sep 05 '15 at 21:22