Date Format

i have looked at many examples online and I am not understanding how to format the date when using the following code:

[php]<?php
$dblink = mysql_connect(‘host’, ‘username’, ‘password’);
mysql_select_db(‘db’);
$query = ‘SELECT * FROM comments ORDER BY date DESC LIMIT 0, 100 ‘;
$result = mysql_query($query);
while($rec = mysql_fetch_object($result)) {
echo ‘

’.$rec->name.’: ‘.$rec->comment.’
Blabbed on ‘.$rec->date.’
’;
}
?>[/php]

The date comes back like this: 2011-11-25 14:21:12
I need it to look like this: 11/25/2011 at 2:21pm

Thanks!!!

Hello,

You can use PHP’s date() function to format your output of dates. The syntax that you can to use in your code is:

[php]date(‘m/d/Y \a\t g:ia’, $rec->date);[/php]

Here it is in your example:

[php]<?php

$dblink = mysql_connect(‘host’, ‘username’, ‘password’);
mysql_select_db(‘db’);
$query = ‘SELECT * FROM comments ORDER BY date DESC LIMIT 0, 100 ‘;
$result = mysql_query($query);
while($rec = mysql_fetch_object($result)) {
echo ‘

’.$rec->name.’: ‘.$rec->comment.’
Blabbed on ‘.date(‘m/d/Y \a\t g:ia’, $rec->date).’
’;
}

?>[/php]

Hope this helps.

Perfect!!! Thanks!!!

Sponsor our Newsletter | Privacy Policy | Terms of Service