CodeIgniter Forums
Formatting a date in a view. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Formatting a date in a view. (/showthread.php?tid=58512)



Formatting a date in a view. - El Forum - 06-18-2013

[eluser]gscodeignitor_india[/eluser]
I am absolutely new to Codeignitor and trying to learn some basic skills.

I could do a few things like displaying data stored in MySql etc.

However I could not format the date. ( I need to format the default date to dd-mm-yyyy format )

Please help...

Following is my customer_view file.

<html>


<body>

<table border="3">
<tr>
<th>Customer</th>
<th>P.O. Number</th>
<th>PODate</th>
<th>Config</th>
<th>Qty</th>
<th>Order Value</th>
<th>OA Number</th>
<th>Warranty</th>

</tr>
&lt;?php

foreach($records as $orderdata){
echo "<tr>";

echo "<td>". $orderdata->customer."</td>";
echo "<td>". $orderdata->ponumber."</td>";
echo "<td>". $orderdata->podate. "</td>";
echo "<td>". $orderdata->config ."</td>";
echo "<td>". $orderdata->qty ."</td>";
echo "<td>". $orderdata->ordervalue ."</td>";
echo "<td>". $orderdata->oanumberdate."</td>";
echo "<td>". $orderdata->warranty ."</td>";
echo "</tr>";
}

?&gt;
</table>
&lt;/body&gt;
&lt;/html&gt;

The output I am getting as follows.

The date output for datefield ( in my case it is 'podate' ) is in Year-Month-Date-time format where as I need to display date in Date-Month-Year Format.


Any help is appreciated.

Thanks in advance.



Formatting a date in a view. - El Forum - 06-18-2013

[eluser]PravinS[/eluser]
you can use DATE_FORMAT function of mysql

check: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format


Formatting a date in a view. - El Forum - 06-18-2013

[eluser]gscodeignitor_india[/eluser]
I have tried that as well.

example

echo "<td>". $orderdata->date_format(podate, '%d %m,%Y'). "</td>";
&
echo "<td>". date_format($orderdata->podate, '%d %m,%Y'). "</td>";

But does not help...

Rather page returns with error.

Please help.



Formatting a date in a view. - El Forum - 06-19-2013

[eluser]PravinS[/eluser]
you need to use DATE_FORMAT function of mysql in you MySQL query, not in view file

but if you want to use in your view then you need to use date() function of php, for that you have to convert your "podate" value in timestamp format and use the date function

http://php.net/manual/en/function.date.php


Formatting a date in a view. - El Forum - 06-19-2013

[eluser]Pert[/eluser]
Code:
&lt;?= date('%d %m,%Y', strtotime($orderdata->podate)) ?&gt;



Formatting a date in a view. - El Forum - 06-19-2013

[eluser]gscodeignitor_india[/eluser]
Thanks Pert ....

This line has made the trick...

echo "<td>". date('d/M/Y', strtotime($orderdata->podate)) ."</td>";Thanks ..

Many thanks again.

Regards,