PDA

View Full Version : Display date with PHP


Pokeguy
12-11-2000, 04:18 AM
Hi, i have a database of around 1000 stock prices at a different dates and i want to display them all in a table.

The problem is, the dates displayed in column 1 of the table is in the form
YYYY-MM-DD
I want to change the form to MM DD, YYYY.

This following section from a script displays the dates correctly, but not in the format i want.

{
printf(&quot;<tr><td>%s</td><td>%s/</td><td>%s</td></tr>\n&quot;,
$myrow[&quot;Date&quot;]), $myrow[&quot;Point&quot;], $myrow[&quot;Low&quot;]);
}

I tried changing that part to the following, but now, instead of giving me a list
of dates(which go from earliest to present), it gives me the exactly SAME date over and over again(eg. repeats Jan 1, 1998 over and over again in the loop).

what's wrong with this script?
{
printf(&quot;<tr><td>%s</td><td>%s/</td><td>%s</td></tr>\n&quot;,
date('M D Y',$myrow[&quot;Date&quot;]), $myrow[&quot;Point&quot;], $myrow[&quot;Low&quot;]);
}

anyone know why??

Thanx in advance[nbsp]

Arthur
12-11-2000, 04:50 AM
The date() function of PHP takes a timestamp as its argument. (A timestamp is the number of seconds between 1-1-1970 and the date)
So, you can either get MySQL to give a Unix timestamp instead of the date;

$query = &quot;SELECT UNIX_TIMESTAMP(Date) as timestamp from TABLE&quot;

and then use date() to format it;

<?
date('M D Y',$myrow[&quot;timestamp&quot;]);
?>

or you can just re-arrange the date you get from MySQL;

<?
[nbsp][nbsp] $date = $myrow[&quot;Date&quot;];[nbsp][nbsp][nbsp][nbsp] // e.g. &quot;2000-04-05&quot;
[nbsp][nbsp] $temp = explode(&quot;-&quot;,$date);[nbsp][nbsp]// gets you an array with 3 elements; &quot;2000&quot;, &quot;04&quot;, &quot;05&quot;
[nbsp][nbsp] $date = $temp[1].&quot;-&quot;.$temp[2].&quot;-&quot;.$temp[0];[nbsp][nbsp]// print it as &quot;04-05-2000&quot;
?>

HTH

Arthur

Pokeguy
12-11-2000, 04:11 PM
You idea works fine, but now I can't see any other data.

I changed my script to the following :

<?php

$db = mysql_connect(&quot;localhost&quot;, &quot;root&quot;, &quot;password&quot;);

mysql_select_db(&quot;database&quot;,$db);

$result = mysql_query(&quot;SELECT UNIX_TIMESTAMP(Date) as Date FROM datatable WHERE Bond = 'Fixed' AND Date > '1998-01-06' AND Date < '1998-07-17'&quot;);
if($myrow = mysql_fetch_array($result))

{

echo &quot;<table border=1>\n&quot;;

do

{

printf(&quot;<tr>

<td>%s</td><td>%s</td><td>%s</td>
</tr>\n&quot;, date('m d Y', $myrow[&quot;Date&quot;]), $myrow[&quot;Bond&quot;], $myrow[&quot;Low&quot;]);

}

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

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

}

?>

Now when I run this script, all I see is the column of dates, but I don't see any other columns(prices, etc.)

Before, I would see three columns, now it only shows one.

Anyone know why?

thanx

PaulKroll
12-11-2000, 05:05 PM
Well, the SELECT is choosing one thing to select: the Date. In whatever query you used before, you must have had the individual items chosen, OR used &quot;*&quot; for &quot;Select every field.&quot;

If the fields are in fact &quot;Bond&quot; and &quot;Low&quot; then change the start of the SELECT to &quot;SELECT Bond,Low,UNIX_TIMESTAMP(Date) as Date&quot;.

http://www.mysql.com has some excellent docs and examples, so if you're gonna get into MySQL-ing, you might wanna take a gander. :)