views:

3546

answers:

4

Hi I have looked at a question already posted about this and tried to adapt my code to do a simlar thing. Basicly I have a field in my mysql database that has the date in this format yyyy-dd-mm i need to the format to be dd-mm-yyyy. Currently my code is getting stuck on the line echo "<td><strong> ("d/m/y",". $row['date'] .")</strong></td>"; and reads this Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'. Not sure how to fix this any ideas I would be greatful.

$result = mysql_query("SELECT * FROM dbGigs WHERE CURDATE() < date ORDER BY date");

echo "<table class=\"gigs\">
<tr>
<th>Venue</th>
<th>Town</th>
<th>Date</th>
<th>Time</th>
<th>Status</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td><strong>" . $row['venue'] . "</strong></td>";
  echo "<td><strong>" . $row['town'] . "</strong></td>";
  echo "<td><strong> ("d/m/y",". $row['date'] .")</strong></td>";
  echo "<td><strong>" . $row['time'] . "</strong></td>";
  echo "<td><strong>" . $row['status'] . "</strong></td>";
  echo "</tr>";
+7  A: 

First things first

  echo "<td><strong> ("d/m/y",". $row['date'] .")</strong></td>";

This line looks wrong; I suspect it ought to read something like

  echo "<td><strong>" . date( "d/m/y", $row['date'] ) . "</strong></td>";

Note the missing function call and concatenation operators. Incidentally, while this is syntactically correct (so far as I can see), it may not do what you're looking for, depending upon what $row['date'] actually contains - it needs to be a Unix timestamp for date() to grok it properly.

To obtain the date as a Unix timestamp, you can use the MySQL function UNIX_TIMESTAMP() when selecting the date, e.g.

SELECT *, UNIX_TIMESTAMP(date) AS date_ts FROM ...

You can then access $row['date_ts'] as normal, and pass it into the date() function for formatting. Alternatively, you can use something like strtotime() to parse the current value being returned in order to get a timestamp from that.

A further alternative is to have MySQL format the date on your behalf, using its DATE_FORMAT() function; again, you can access this value returned from the query and print it out.

Rob
Yes you are correct I doesn't do what I want haha. The date stored in that that field date is a mysql date field and asks for the date in the format yyyy-dd-mm which I don't think is very easy for the user to understand when viewed on a webpage. Would you know how to do that? Thanks.
Cool Hand Luke UK
Updated the answer with a few ideas.
Rob
A: 

You have to call the date function:

echo "<td><strong>".date("d/m/y", $row['date'])."</strong></td>";
Gumbo
A: 

$res = mysql_query('SELECT dte FROM vtable'); $row = mysql_fetch_array($res); print date('d M Y', strtotime($row['dte']));

A: 

Well where as I think

Use it like echo "".date("d/m/y", strtotime($row['date']))."";

Manoj kumar