user957363
user957363

Reputation:

How can I display only certain characters from a mysql_fetch_array

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)
if ($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...

Upvotes: 0

Views: 355

Answers (5)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

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.

Upvotes: 0

Vikk
Vikk

Reputation: 3363

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)

Upvotes: 0

K6t
K6t

Reputation: 1845

echo date("m-d",srtotime($array[event_date]);

//09-05

Upvotes: 1

Jos&#233; P. Airosa
Jos&#233; P. Airosa

Reputation: 368

You can achieve this with the substr() PHP function: http://pt.php.net/manual/en/function.substr.php

Example:

substr($array[event_date],5);

Upvotes: 1

Alan Moore
Alan Moore

Reputation: 6575

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>";

Upvotes: 0

Related Questions