Forum Moderators: coopster

Message Too Old, No Replies

Problem with SQL Date

date format

         

edpudol

5:37 am on Sep 2, 2004 (gmt 0)

10+ Year Member



Hello fellows!

I have problem with my viewing my sql date format

Use this when I save the date to my mySQL table:

$cdate=date("m/d/y");
$dateend = date("m/d/Y", strtotime("+3 days"));

mysql_query("INSERT INTO Mtable
VALUES ('$cdate',
'$dateend')") or die(mysql_error());

The above code works, but when I retrieve it and try to view the result using the following code:

echo $myrow[dateend];
echo $myrow[datepost];

I got this results :
for dateend 2009-04-20
and for datepost 2009-01-04

I tried to use this code:

echo date ("F j, Y",$myrow[dateend]);
echo date("F j, Y",$myrow[datepost]);

but I get the following result for both code:

December 31, 1969

Need your help, thank you in advance

dreamcatcher

8:07 am on Sep 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You haven`t mentioned how exactly you want the date to display. :)

Have you tried using DATE_FORMAT?

edpudol

10:42 am on Sep 2, 2004 (gmt 0)

10+ Year Member



I just want to display date in format like this September 2, 2004.

When I save my record I don't use the date_format function.

dreamcatcher

1:39 pm on Sep 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry if I confused you. You use DATE_FORMAT when you display your data.

Something like this:


$query = ("SELECT DATE_FORMAT(datepost, '%F %j, %Y') as display_time FROM tablename LIMIT 1") or die(mysql_error());
$row = mysql_fetch_object($query);

echo $row->display_time;

You may not need mysql_fetch_object or the limit clause, they are just here for examples.

You need to use DATE_FORMAT because the DATE field by default displays in the US format. Alternatively you can just store the value of F j Y in a varchar field.

edpudol

2:24 am on Sep 3, 2004 (gmt 0)

10+ Year Member



Thanks for the reply, the code works properly when I use it but what about if I would like to display all the fields record inside my table? I use the * to display all fields how could I do that with the date_format function?

Once again thanks.

dreamcatcher

6:46 am on Sep 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use mysql_fetch_array() to pull all the rows.