Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting Dates within a Query?

Status
Not open for further replies.

NigeB

Technical User
Nov 29, 2000
53
GB
Below is listed a simply query that is integrated within a web page, the
problem I have is that the 3rd %s I wish to display as the month name, the
data_updated field is a timestamp, I just need a little help on how or where
to put the DATE_FORMAT or MONTHNAME criteria within the query.

Any help would be greatly appreciated.

Nige.

MySQL query__________________________

echo &quot;<table width=100% border=0>\n&quot;;

while ($myrow = mysql_fetch_array($result)){

printf(&quot;<tr><td align=left><a href=%s><b><font size=3>%s</font></b></a>
%s</td></tr>
<tr><td align=left><b><i>%s</i></b><td>
</tr>\n&quot;, $myrow[&quot;url&quot;], $myrow[&quot;headline&quot;],
$myrow[&quot;data_updated&quot;],$myrow[&quot;details&quot;]);

}

echo &quot;</table>\n&quot;;
 
assuming your data field is a unix timestamp you should be able to do

date(&quot;M&quot;, $myrow[&quot;data_updated&quot;]);

F will give you the full months name

heres some others


a - &quot;am&quot; or &quot;pm&quot;

A - &quot;AM&quot; or &quot;PM&quot;

d - day of the month, 2 digits with leading zeros; i.e. &quot;01&quot; to &quot;31&quot;

D - day of the week, textual, 3 letters; i.e. &quot;Fri&quot;

F - month, textual, long; i.e. &quot;January&quot;

h - hour, 12-hour format; i.e. &quot;01&quot; to &quot;12&quot;

H - hour, 24-hour format; i.e. &quot;00&quot; to &quot;23&quot;

g - hour, 12-hour format without leading zeros; i.e. &quot;1&quot; to &quot;12&quot;

G - hour, 24-hour format without leading zeros; i.e. &quot;0&quot; to &quot;23&quot;

i - minutes; i.e. &quot;00&quot; to &quot;59&quot;

j - day of the month without leading zeros; i.e. &quot;1&quot; to &quot;31&quot;

l (lowercase 'L') - day of the week, textual, long; i.e. &quot;Friday&quot;

L - boolean for whether it is a leap year; i.e. &quot;0&quot; or &quot;1&quot;

m - month; i.e. &quot;01&quot; to &quot;12&quot;

n - month without leading zeros; i.e. &quot;1&quot; to &quot;12&quot;

M - month, textual, 3 letters; i.e. &quot;Jan&quot;

s - seconds; i.e. &quot;00&quot; to &quot;59&quot;

S - English ordinal suffix, textual, 2 characters; i.e. &quot;th&quot;, &quot;nd&quot;

t - number of days in the given month; i.e. &quot;28&quot; to &quot;31&quot;

U - seconds since the epoch

w - day of the week, numeric, i.e. &quot;0&quot; (Sunday) to &quot;6&quot; (Saturday)

Y - year, 4 digits; i.e. &quot;1999&quot;

y - year, 2 digits; i.e. &quot;99&quot;

z - day of the year; i.e. &quot;0&quot; to &quot;365&quot;

Z - timezone offset in seconds (i.e. &quot;-43200&quot; to &quot;43200&quot;)


Andres Jugnarain
Wireless Editor
 
Andres,

Thanks for the guidance with the UNIX timestamp, it worked well.

I have another query with a simila layout, but this one uses a date yyyy-mm-dd format in the table, is there a way to specify the display format within a query.

Regards

Nige B
 
in your sql string you could specify:

select unix_date as UNIX_TIMESTAMP(yourdatecol), col2, col3......FROM......etc

there are quite a few built in functions within mysql that you can use in this way - see the full documentation on at





Andres Jugnarain
Wireless Editor
 
Andres,

Am I missing the point here? My query is as below, but returns an error (note the query works OK prior to my addition of the code you suggested, I think I must have something in wrong, any ideas?

TIA

Nige B

Error ----
Warning: Supplied argument is not a valid MySQL result resource in /home/nigel/public_html/database/test.php on line 67 No Records!

The query is as follows ----
$result = mysql_query(&quot;SELECT Points.Dogs_BFA_No, Points.FB_Points, unix_date as UNIX_TIMESTAMP(Venues.Date), Venues.Venue, Venues.Date, Teams.Team, Competition_Types.Event_Type
FROM Venues
LEFT JOIN Points ON Venues.Tournament_ID = Points.Tournament_ID
LEFT JOIN Teams ON Points.TRN = Teams.TRN
LEFT JOIN Competition_Types ON Points.Event_Type = Competition_Types.Event_Type_Ident
WHERE Points.Dogs_BFA_No = '0014B' AND Venues.Date <'2001-01-01'AND Venues.Date >'2000-01-01'
ORDER BY Venues.Date DESC&quot;, $db);


if ($myrow = mysql_fetch_array($result)) {

echo &quot;</p>
<table align=center border=1 cellpadding=3>\n&quot;;

echo &quot;<tr><td bgcolor=#FFFF00 width=70 align=center><font size=4 color=#000080><b>unix</b></font></td>
<td bgcolor=#FFFF00 width=70 align=center><font size=4 color=#000080><b>Date</b></font></td>
<td bgcolor=#FFFF00 width=80 align=center><font size=4 color=#000080><b>Venue</b></font></td>
<td bgcolor=#FFFF00 width=70 align=center><font size=4 color=#000080><b>Points</b></font></td>
<td bgcolor=#FFFF00 width=100 align=center><font size=4 color=#000080><b>Class</b></font></td>
<td bgcolor=#FFFF00 width=150 align=center><font size=4 color=#000080><b>Flyball Team</b></font></td></tr>\n&quot;;



do {
printf(&quot;<tr>
<td align=center bgcolor=#000000><font size=2 color=#ffffff>%s</font></td>
<td align=center bgcolor=#000000><font size=2 color=#ffffff>%s</font></td>
<td align=center bgcolor=#000000><font size=2 color=#ffffff>%s</font></td>
<td align=center bgcolor=#000000><font size=2 color=#ffffff>%s</font></td>
<td align=center bgcolor=#000000><font size=2 color=#ffffff>%s</font></td>
<td align=center bgcolor=#000000><font size=2 color=#ffffff>%s</font></td></tr>\n&quot;,
date(&quot;M&quot;,$myrow[&quot;UNIX_TIMESTAMP&quot;]),$myrow[&quot;Date&quot;], $myrow[&quot;Venue&quot;], $myrow[&quot;FB_Points&quot;], $myrow[&quot;Event_Type&quot;], $myrow[&quot;Team&quot;]);



} while ($myrow = mysql_fetch_array($result));


echo &quot;</table>\n&quot;;

} else {

echo &quot;No Records!&quot;;

}
?>
 
try re-arranging the order that you specify the columns in sql query so that you specify venues.date before you specify unix_date as UNIX_TIMESTAMP(Venues.Date)

i think that might make a difference, but I havent checked.

.... actually on second thoughts i think i gave you the code back to front!

it should be UNIX_TIMESTAMP(Venues.Date) as unix_date Andres Jugnarain
Wireless Editor
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top