Page 1 of 1

Date Format

Posted: Thu Jun 12, 2008 4:01 pm
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.

Re: Date Format

Posted: Thu Jun 12, 2008 4:02 pm
by xitura
Look at row 15, you assign a new value to $news_date.

Re: Date Format

Posted: Thu Jun 12, 2008 4:06 pm
by tchester
I know, I tried to use date_format but am unable to get anything from that function

Re: Date Format

Posted: Thu Jun 12, 2008 4:10 pm
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

Re: Date Format

Posted: Thu Jun 12, 2008 4:12 pm
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?!

Re: Date Format

Posted: Thu Jun 12, 2008 4:17 pm
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

Re: Date Format

Posted: Thu Jun 12, 2008 4:25 pm
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

Re: Date Format

Posted: Thu Jun 12, 2008 4:34 pm
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>
";
 
?>
 

Re: Date Format

Posted: Fri Jun 13, 2008 4:34 am
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

Re: Date Format

Posted: Fri Jun 13, 2008 4:39 am
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.

Re: Date Format

Posted: Fri Jun 13, 2008 1:33 pm
by RobertGonzalez
Format the date on the DB and be done with it.

Re: Date Format

Posted: Sun Jun 15, 2008 9:41 pm
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.