Need help printing formatted mysql datetime field |
[eluser]intoitdaily[/eluser]
Hey guys, new to CodeIgniter, (this is my first post) but I imagine I'll be using this for as long as I develop websites. I LOVE IT! Anyway, as the title indicates, I can't seem to print a formatted mysql datetime field. Code: <?php print $row->date; ?> Code: <?php print date_format($row->date, 'g:i a - M jS, Y'); ?> I also tried Code: <?php print date('g:i a - M jS, Y', $row->date); ?> Either way. CodeIgniter gives me the error Code: A PHP Error was encountered Can I get some help or suggestions? If it would make things easier I can change the mysql table field to be a timestamp, but it doesn't make sense why I wouldn't be able to format it as is. Thanks in advanced!
[eluser]Thorpe Obazee[/eluser]
Code: <?php echo strftime('%A', strtotime('2009-04-26 20:13:33'));?> Try using strftime(). Although the result you get from the database could already be formatted via MySQL.
[eluser]intoitdaily[/eluser]
Thanks bargainph! This is what I'm using now: Code: <?php print strftime('%l:%M%P - %b %e, %Y', strtotime($row->date)); ?> The only thing I wish it had was a suffix for the numerical date, like 31st, 2nd, or 5th... Again, many thanks for the quick reply!
[eluser]umefarooq[/eluser]
if you are fetching records from mysql using query than you can use mysql's date_format() function which will be quite handy to you and give you proper formatted date check out here http://dev.mysql.com/doc/refman/5.1/en/d...ate-format
[eluser]Zeeshan Rasool[/eluser]
Did you try like this: Code: "Select blog_id,DATE_FORMAT(post_pub_date, '%M %d %Y') AS post_pub_date,blog_name
[eluser]umefarooq[/eluser]
getting formatted date from mysql is really better than from PHP
[eluser]Zeeshan Rasool[/eluser]
[quote author="umefarooq" date="1241211573"]getting formatted date from mysql is really better than from PHP[/quote] Infact im very alurgic from strtotime function ;-) its very easy to get the date format from mysql function .
[eluser]Yorick Peterse[/eluser]
As stated by others, using a correctly formatted date that's being retrieved from MySQL is better. Probably the best way, or atleast my favourite way, is to store an Unix timestamp as the date in your MySQL table and then use the mdate() function to create a date in a specified format, for example: Code: <?php In this case $row->date fetches the Unix Timestamp from the MySQL database.
[eluser]Zeeshan Rasool[/eluser]
[quote author="Yorick Peterse" date="1241286387"]As stated by others, using a correctly formatted date that's being retrieved from MySQL is better. Probably the best way, or atleast my favourite way, is to store an Unix timestamp as the date in your MySQL table and then use the mdate() function to create a date in a specified format, for example: Code: <?php In this case $row->date fetches the Unix Timestamp from the MySQL database.[/quote] Well this is also a good method to get date.... |
Welcome Guest, Not a member yet? Register Sign In |