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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date format 2

Status
Not open for further replies.

d0nny

IS-IT--Management
Dec 18, 2005
278
GB
OK, I've played around with this for long enough!

I'm using statements in my PHP files and this is what I have:
Code:
$result = mysql_query("select * from lunch where category='lunch' and date>=CURDATE()");
while ($row = mysql_fetch_assoc($result)) {
      echo '<div class="lunchItem">';
      echo '<div class="lunchDateHeader">';
      echo $row[date];
      echo '</div>';
      echo '<div class="lunchItems">';
      echo $row[maincourse].'<br />'.$row[pudding];
      echo '</div>';
      echo '</div>';
}
What I want to do do though is format the date.
I want the date to look like this

Monday 21 September

But I have no idea how to format this associative array.
 
Donny you should enquote your array keys.

date should not be an associative array. i assume it is a mysql timestamp.

there are two ways to achieve what you want. a php method and a mysql method

Code:
$row['date'] = date( 'l j F', strtotime($row['date']));

Code:
$result = mysql_query("select *, date_format( date , '%W %e %M') as fDate from lunch where category='lunch' and date>=CURDATE()");

while ($row = mysql_fetch_assoc($result)) {
      $row['date'] =& $row['fDate']; //overwrite the date element
      //as normal
 

Thanks Vragabond, but I had already read that. It does give you some theory on how to use the DATE_FORMAT functions, but as I was using '*' to retrieve my data into an associative array, I was unsure how I could format the date once it was in that array.

As ever, thanks jpadie. I was on those lines of though about the PHP solution, but I could work out how to format the date in my WHILE loop. Worked that out now.
And I didn't know I could request all field ('*') in my sql and also a specific field like date?? I thought I had to use '*' or then specify all the fields I wanted.

Which do you think is the better method? I've used the SQL method for now as I understand that better (;-)), but which is better?
 
Hi

d0nny said:
Which do you think is the better method?
That may depend on some details of the application. Personally I also prefer to calculate/format dates in the SQL command, but there are cases when is better to do it in PHP :
[ul]
[li]the application is localized[/li]
[li]want to extract other information too, for example to display weekend dates with red[/li]
[li]want human-friendly display for near dates as "yesterday"/"today"/"tomorrow"[/li]
[/ul]


Feherke.
 
i agree with feherke. but i tend towards using php only and keeping my sql as close to ansi sql as possible as i now often code for database independence (able to use mysql, sqlite etc etc. if an app is to be forever tied to mysql, my leaning would be towards using mysql for the job, subject to feherke's exceptions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top