Welcome Guest, Not a member yet? Register   Sign In
Need help printing formatted mysql datetime field
#1

[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; ?>

Output:
2009-04-26 20:13:33
but when I try something like:
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

Severity: Warning

Message: date_format() expects parameter 1 to be DateTime, string given

Filename: views/blog_id_view.php

Line Number: 19

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!
#2

[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.
#3

[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!
#4

[eluser]Dam1an[/eluser]
If you look at PHP date, you'll see that 'S' (upper case) gives the date suffix
#5

[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
#6

[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
from Blogs";
direct get formatted date from db.
#7

[eluser]umefarooq[/eluser]
getting formatted date from mysql is really better than from PHP
#8

[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 .
#9

[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
echo mdate("%d-%m-%Y",$row->date);
?>

In this case $row->date fetches the Unix Timestamp from the MySQL database.
#10

[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
echo mdate("%d-%m-%Y",$row->date);
?>

In this case $row->date fetches the Unix Timestamp from the MySQL database.[/quote]

Well this is also a good method to get date....




Theme © iAndrew 2016 - Forum software by © MyBB