Date Format

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tchester
Forum Newbie
Posts: 10
Joined: Thu Jun 12, 2008 3:59 pm

Date Format

Post by tchester »

I Have the following code:

Code: Select all

 
<?
 
$result = mysql_query("SELECT * FROM news");
 
$num = mysql_num_rows($result);
 
 
$news_date = mysql_result($result,$i,'news_date');
$news_title = mysql_result($result,$i,'news_title');
 
echo "
            <tr>
                <td>";
                $news_date = date("F j, Y"); echo "$news_date<br><br>$news_title<br><br></td>
            </tr>
            <tr>
                <td class='style10' style='width: 90%; height: 2px'></td>
            </tr>
";
 
?>
 
I want to output a date that comes from a database but it keeps returning the current date

Thanks in advance for your help.
xitura
Forum Newbie
Posts: 20
Joined: Fri Sep 07, 2007 11:25 am

Re: Date Format

Post by xitura »

Look at row 15, you assign a new value to $news_date.
tchester
Forum Newbie
Posts: 10
Joined: Thu Jun 12, 2008 3:59 pm

Re: Date Format

Post by tchester »

I know, I tried to use date_format but am unable to get anything from that function
tchester
Forum Newbie
Posts: 10
Joined: Thu Jun 12, 2008 3:59 pm

Re: Date Format

Post by tchester »

I've also tried:

Code: Select all

 
<?
 
$result = mysql_query("SELECT * FROM news");
 
$num = mysql_num_rows($result);
 
 
$news_date = mysql_result($result,$i,'news_date');
$news_title = mysql_result($result,$i,'news_title');
 
$date = date_format($news_date, '%M');
 
echo "
            <tr>
                <td>$date<br><br>$news_title<br><br></td>
            </tr>
            <tr>
                <td class='style10' style='width: 90%; height: 2px'></td>
            </tr>
";
 
?>
 
but get the error:

Fatal error: Call to undefined function: date_format() in D:\sites\envoydata\test\index2.php on line 213 where I define $date
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: Date Format

Post by deejay »

what format is the date coming back from the database in . If I understand correctly your problem is then turning that date into the format you want?

http://uk.php.net/manual/en/function.mktime.php might be the answer?!
tchester
Forum Newbie
Posts: 10
Joined: Thu Jun 12, 2008 3:59 pm

Re: Date Format

Post by tchester »

basically the date comes from the server as:

2008-06-02

While I want to format it to look like

June 2nd, 2008
tchester
Forum Newbie
Posts: 10
Joined: Thu Jun 12, 2008 3:59 pm

Re: Date Format

Post by tchester »

Code: Select all

 
$news_date = mysql_result($result,$i,'news_date');
$news_title = mysql_result($result,$i,'news_title');
 
$date = date('M j, Y',$news_date);
 
echo "
            <tr>
                <td>$date<br><br>$news_title<br><br></td>
            </tr>
            <tr>
                <td class='style10' style='width: 90%; height: 2px'></td>
            </tr>
";
 
This gives the date: Dec 31, 1969

Formatted correctly but wrong date
tchester
Forum Newbie
Posts: 10
Joined: Thu Jun 12, 2008 3:59 pm

Re: Date Format

Post by tchester »

Final code:

Code: Select all

 
<?
$result = mysql_query("SELECT news_title, date_format(news_date, '%b %D, %Y') as date FROM news");
 
$num = mysql_num_rows($result);
 
 
$date = mysql_result($result,$i,'date');
$news_title = mysql_result($result,$i,'news_title');
 
echo "
            <tr>
                <td>$date<br><br>$news_title<br><br></td>
            </tr>
            <tr>
                <td class='style10' style='width: 90%; height: 2px'></td>
            </tr>
";
 
?>
 
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: Date Format

Post by dbemowsk »

Use this:

Code: Select all

 
$news_date = date("F j, Y", strtotime($news_date));
 
The issue is that the date() function expects the second parameter to be in unixtime. Since your database is outputting in a string format, you need to convert to the unix date/time format which is where the strtotime() function comes in. It takes a string formatted date and converts it to unixtime.

Hope that helps
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Date Format

Post by VladSun »

I think it's preferable to do such things by using the DB engine and not in the PHP code. So, I find tchester's solution better than the others.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Date Format

Post by RobertGonzalez »

Format the date on the DB and be done with it.
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: Date Format

Post by dbemowsk »

VladSun wrote:I think it's preferable to do such things by using the DB engine and not in the PHP code. So, I find tchester's solution better than the others.
I do agree with this (somewhat). The reason I say somewhat is that in my framework I have user configurable options that allow the user to select different date formats to suit user preference. With this, I typically store my dates in unix time format which then makes the PHP conversion easier by eliminating the need for the strtotime() function.

It would take a bit of conversion to my framework, but I suppose I could still have user configurable options that change the SQL query to retrieve the date in the format I want instead of changing a PHP date format string, but would I really gain that much from it?

It is still a good idea for techster to know the issues of formatting a date in PHP.
Post Reply