I am trying to create a list of upcoming events by pulling data from an event table in mysql. Currently the output looks like this:
Upcoming Events 2011-09-21 School Walkathon 2011-10-06 Open House 2011-10-17 PTA Meeting 2011-11-14 PTA Meeting 2011-09-30 Staff Development Day
However, I would like the date to display only the month and day. Here is my code:
<?php
//---Connect To Database
$hostname="localhost";
$username="root";
$password="urbana116";
$dbname="vcalendar3";
mysql_connect($hostname,$username, $password) OR DIE ("<html><script language='JavaScript'<alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($dbname);
//---Query the database into dataset ($result) stop after the fifth item
$query = 'SELECT event_id, event_title, event_date FROM events WHERE event_date >= CURDATE() ORDER BY category_id != 9, category_id != 7, event_date ASC limit 5';
$result = mysql_query($query);
//---Populate $array from the dataset then display the results (or not)
i开发者_运维百科f ($result) {
while ($array= mysql_fetch_assoc($result)) {
echo "<TR><td>".$array[event_date]."</TD><td><a href='/vcalendar/event_view.php?event_id=".$array[event_id]."'>".$array[event_title]."</a></td></tr>";
}
}else{
echo "No results";
}
?>
I've tried to select just certain characters from the event_date array like this:
echo "<TR><td>".$array[event_date][5]."</TD><td><a href='/vcalendar/event_view.php?event_id=".$array[event_id]."'>".$array[event_title]."</a></td></tr>";
but that only displays the 5th character, not characters 5-9...
You can achieve this with the substr() PHP function: http://pt.php.net/manual/en/function.substr.php
Example:
substr($array[event_date],5);
echo date("m-d",srtotime($array[event_date]);
//09-05
How about:
echo "<TR><td>".substr($array[event_date], 5)."</TD><td><a href='/vcalendar/event_view.php?event_id=".$array[event_id]."'>".$array[event_title]."</a></td></tr>";
You should format the date.
use date('m-d',strtotime($array[event_date]))
This way, you have the flexibility to display it any way you like (say September 21 instead of 09-21)
For more freedom, you can use this:
echo "<tr><td>".date("m-d",strtotime($array['event_date']))."</td> ...
Change the m-d
around to whatever format you want. For example, F dS
would give something like September 21st
.
精彩评论