Forum Moderators: coopster

Message Too Old, No Replies

date formatting in PHP/MYSQL using DWMX 2004

         

fwordboy

3:16 pm on Jan 26, 2004 (gmt 0)

10+ Year Member



i no this question has been asked about 5 gazillion times all over the web but the answer i'm looking for doesn't seem to be available.
i want to convert the timestamp e.g. 20040126 etc into DD/MM/YY or even e.g. Monday 26th January 2004.

the sql to convert the timestamp to dd/mm/yy is:
select DATE_FORMAT(posted, '%D %M, %Y') from news;

but trying to display the page is a nightmare. errors such as:
DW MX 2004 produces this code @ the top of the page for the query:
mysql_select_db($database_mewannews, $mewannews);
$query_getdate = "SELECT DATE_FORMAT(posted, '%e %M, %Y ') FROM news";
$getdate = mysql_query($query_getdate, $mewannews) or die(mysql_error());
$row_getdate = mysql_fetch_assoc($getdate);
$totalRows_getdate = mysql_num_rows($getdate);

and this code to display the date
<?php echo $row_getdate['posted'];?>

jatar_k

6:34 pm on Jan 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



but trying to display the page is a nightmare. errors such as:

sorry, but I don't see any errors posted.

Does the DATE_FORMAT select work? If there are errors could you post the specific error and we will see what we can figure out.

fwordboy

10:58 am on Jan 27, 2004 (gmt 0)

10+ Year Member



error:
Notice: Undefined index: posted in c:\inetpub\wwwroot\etc.php on line 106

this is line 106
<?php echo $row_getdate['posted'];?>

coopster

11:40 am on Jan 27, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It is telling you that you are trying to access an index in your array that does not exist. A quick way to indentify errors like this is to use PHP's print_r [php.net] function to print out the array just before your error line and you'll be able to see your issue:

print '<pre>';
print_r($row_getdate);
exit '<pre>'; // I often use exit to halt processing; you can also use print to continue...

jatar_k has a good tip on this. See PHP Troubleshooting [webmasterworld.com] in the PHP Library Forum [webmasterworld.com].

That said, I think you'll find that your error is resolved by simply assigning your variable name to your DATE_FORMAT expression as an alias:


$query_getdate = "SELECT DATE_FORMAT(posted, '%e %M, %Y ') AS posted FROM news";

fwordboy

1:13 pm on Jan 27, 2004 (gmt 0)

10+ Year Member



$query_getdate = "SELECT DATE_FORMAT(posted, '%e %M, %Y ') AS posted FROM news";

superstar!

thats work beautifully. thankyou so much. its always the easy answers that are hard to find.

g1smd

12:38 am on Feb 3, 2004 (gmt 0)