Connecting Tech Pros Worldwide Forums | Help | Site Map

Formatting date stored in mysql table

Newbie
 
Join Date: Jul 2007
Location: Durham, UK
Posts: 23
#1: Jul 23 '07
I have a fixture list stored in a table, date, venue, opposition

table consists of: id, date, venue, opposition.

date is stored as a 'date' yyyy-mm-dd, what i want to do is ouput the table

date, venue, opposition with the date formatted as dd-mm or dd-mm-yyyy

any help would be appreciated.
Thanks

mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 719
#2: Jul 23 '07

re: Formatting date stored in mysql table


Use mysql's DATE_FORMAT() function.
Expand|Select|Wrap|Line Numbers
  1. SELECT id, venue, opposition, DATE_FORMAT(`date`, '%d-%m-%Y') AS required_date FROM table_name
Change your column name from date to some other, date is a mysql keyword.
Newbie
 
Join Date: Jul 2007
Location: Durham, UK
Posts: 23
#3: Jul 23 '07

re: Formatting date stored in mysql table


i don't quite understand how you mean.

I have renamed the 'date' field to 'matchdate'.

here is my original code, could you show me what you mean?
Thanks.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4.  
  5. // no direct access
  6. defined( '_VALID_MOS' ) or die( 'Restricted access' );
  7.  
  8. // Get data records from table. 
  9.  
  10. $result=mysql_query("SELECT * FROM war_fixtures");
  11.  
  12. ?>
  13.  
  14. <table width="600" class="roster_border" cellpadding="0" cellspacing="1" >
  15. <tr bgcolor="#78171e" class="table_header">
  16. <td valign="top"><strong>Date</strong></td>
  17. <td valign="top"><strong>Venue</strong></td>
  18. <td valign="top"><strong>Opposition</strong></td>
  19. <td valign="top"><strong>Competition</strong></td>
  20. <td valign="top"><strong>Face Off</strong></td>
  21. <td valign="top"><strong>F</strong></td>
  22. <td valign="top"><strong>A</strong></td>
  23. </tr>
  24.  
  25. <?
  26. // Make a variable "$num" with a value "0". 
  27. $num=0;
  28.  
  29. // Do while loop for out put records. 
  30. while($row=mysql_fetch_assoc($result)){
  31.  
  32.  
  33. ?>
  34. <tr class="table_content" bgcolor="<?=(++$num%2==0 ? "#E6D78E" : "#dcc55d")?>">
  35. <td><? echo $row['matchdate']; ?></td>
  36. <td><? echo $row['venue']; ?></td>
  37. <td><? echo $row['opposition']; ?></td>
  38. <td><? echo $row['competition']; ?></td>
  39. <td><? echo $row['faceoff']; ?></td>
  40. <td><? echo $row['for']; ?></td>
  41. <td><? echo $row['against']; ?></td>
  42. </tr>
  43.  
  44. <? 
  45. // End while loop. 
  46.  
  47. // Close database connection. 
  48. mysql_close();
  49. ?>
  50. <? echo '</table>'; ?>
  51.  
  52.  
  53.  
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Jul 23 '07

re: Formatting date stored in mysql table


Quote:

Originally Posted by keeps21

i don't quite understand how you mean.

I have renamed the 'date' field to 'matchdate'.

here is my original code, could you show me what you mean?
Thanks.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4.  
  5. // no direct access
  6. defined( '_VALID_MOS' ) or die( 'Restricted access' );
  7.  
  8. // Get data records from table. 
  9.  
  10. $result=mysql_query("SELECT * FROM war_fixtures");
  11.  
  12. ?>
  13.  
  14. <table width="600" class="roster_border" cellpadding="0" cellspacing="1" >
  15. <tr bgcolor="#78171e" class="table_header">
  16. <td valign="top"><strong>Date</strong></td>
  17. <td valign="top"><strong>Venue</strong></td>
  18. <td valign="top"><strong>Opposition</strong></td>
  19. <td valign="top"><strong>Competition</strong></td>
  20. <td valign="top"><strong>Face Off</strong></td>
  21. <td valign="top"><strong>F</strong></td>
  22. <td valign="top"><strong>A</strong></td>
  23. </tr>
  24.  
  25. <?
  26. // Make a variable "$num" with a value "0". 
  27. $num=0;
  28.  
  29. // Do while loop for out put records. 
  30. while($row=mysql_fetch_assoc($result)){
  31.  
  32.  
  33. ?>
  34. <tr class="table_content" bgcolor="<?=(++$num%2==0 ? "#E6D78E" : "#dcc55d")?>">
  35. <td><? echo $row['matchdate']; ?></td>
  36. <td><? echo $row['venue']; ?></td>
  37. <td><? echo $row['opposition']; ?></td>
  38. <td><? echo $row['competition']; ?></td>
  39. <td><? echo $row['faceoff']; ?></td>
  40. <td><? echo $row['for']; ?></td>
  41. <td><? echo $row['against']; ?></td>
  42. </tr>
  43.  
  44. <? 
  45. // End while loop. 
  46.  
  47. // Close database connection. 
  48. mysql_close();
  49. ?>
  50. <? echo '</table>'; ?>
  51.  
  52.  
  53.  

Open the link he posted and read it.
P.S. It's not a bad idea to bookmark the MySQL refMal as well.
Newbie
 
Join Date: Jul 2007
Location: Durham, UK
Posts: 23
#5: Jul 23 '07

re: Formatting date stored in mysql table


Quote:

Originally Posted by r035198x

Open the link he posted and read it.
P.S. It's not a bad idea to bookmark the MySQL refMal as well.

Thanks I've read that and have amended my code, all is showing as expected apart from the column in which the date should be shown, which is showing up empty.

Any ideas?

Thanks

Here is the amended code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4.  
  5. // no direct access
  6. defined( '_VALID_MOS' ) or die( 'Restricted access' );
  7.  
  8. // Get data records from table. 
  9.  
  10. $result=mysql_query("SELECT id, DATE_FORMAT('matchdate','%d-%m-%Y') AS 'matchnight', venue, opposition, competition, faceoff, goalsfor, goalsagainst  FROM war_fixtures");
  11.  
  12. ?>
  13.  
  14. <table width="600" class="roster_border" cellpadding="0" cellspacing="1" >
  15. <tr bgcolor="#78171e" class="table_header">
  16. <td valign="top"><strong>Date</strong></td>
  17. <td valign="top"><strong>Venue</strong></td>
  18. <td valign="top"><strong>Opposition</strong></td>
  19. <td valign="top"><strong>Competition</strong></td>
  20. <td valign="top"><strong>Face Off</strong></td>
  21. <td valign="top"><strong>F</strong></td>
  22. <td valign="top"><strong>A</strong></td>
  23. </tr>
  24.  
  25. <?
  26. // Make a variable "$num" with a value "0". 
  27. $num=0;
  28.  
  29. // Do while loop for out put records. 
  30. while($row=mysql_fetch_assoc($result)){
  31.  
  32.  
  33. ?>
  34. <tr class="table_content" bgcolor="<?=(++$num%2==0 ? "#E6D78E" : "#dcc55d")?>">
  35. <td><? echo $row['matchnight']; ?></td>
  36. <td><? echo $row['venue']; ?></td>
  37. <td><? echo $row['opposition']; ?></td>
  38. <td><? echo $row['competition']; ?></td>
  39. <td><? echo $row['faceoff']; ?></td>
  40. <td><? echo $row['goalsfor']; ?></td>
  41. <td><? echo $row['goalsagainst']; ?></td>
  42. </tr>
  43.  
  44. <? 
  45. // End while loop. 
  46.  
  47. // Close database connection. 
  48. mysql_close();
  49. ?>
  50. <? echo '</table>'; ?>
  51.  
  52.  
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 719
#6: Jul 23 '07

re: Formatting date stored in mysql table


Use this code

[PHP]<?php


// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

// Get data records from table.

$result=mysql_query("SELECT id, DATE_FORMAT(matchdate,'%d-%m-%Y') AS matchnight, venue, opposition, competition, faceoff, goalsfor, goalsagainst FROM war_fixtures");

?>

<table width="600" class="roster_border" cellpadding="0" cellspacing="1" >
<tr bgcolor="#78171e" class="table_header">
<td valign="top"><strong>Date</strong></td>
<td valign="top"><strong>Venue</strong></td>
<td valign="top"><strong>Opposition</strong></td>
<td valign="top"><strong>Competition</strong></td>
<td valign="top"><strong>Face Off</strong></td>
<td valign="top"><strong>F</strong></td>
<td valign="top"><strong>A</strong></td>
</tr>

<?
// Make a variable "$num" with a value "0".
$num=0;

// Do while loop for out put records.
while($row=mysql_fetch_assoc($result)){


?>
<tr class="table_content" bgcolor="<?=(++$num%2==0 ? "#E6D78E" : "#dcc55d")?>">
<td><? echo $row['matchnight']; ?></td>
<td><? echo $row['venue']; ?></td>
<td><? echo $row['opposition']; ?></td>
<td><? echo $row['competition']; ?></td>
<td><? echo $row['faceoff']; ?></td>
<td><? echo $row['goalsfor']; ?></td>
<td><? echo $row['goalsagainst']; ?></td>
</tr>

<?
// End while loop.
}

// Close database connection.
mysql_close();
?>
<? echo '</table>'; ?>[/PHP]

There is a difference between single quote ( ' ) and back tick ( ` ).

You were using single quotes around column names in DATE_FORMAT(). Use back tick or leave it altogether.
[PHP]$result=mysql_query("SELECT id, DATE_FORMAT('matchdate','%d-%m-%Y') AS 'matchnight', venue, opposition, competition, faceoff, goalsfor, goalsagainst FROM war_fixtures");[/PHP]
Newbie
 
Join Date: Jul 2007
Location: Durham, UK
Posts: 23
#7: Jul 23 '07

re: Formatting date stored in mysql table


Works perfectly, thanks for your help and patience with a mysql virgin.
mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 719
#8: Jul 23 '07

re: Formatting date stored in mysql table


Quote:

Originally Posted by keeps21

Works perfectly, thanks for your help and patience with a mysql virgin.

Welcome. You can post here whenever you face any problem.
Reply