We help IT Professionals succeed at work.

Unable to format date - PHP + MySQL

BrighteyesDesign
on
I have set up a simple news section using PHP.

There's two pages. One which shows the news as a list which you then click on 'more' to reach the detail page.

The list page formats the date fine...http://www.mjhayward.co.uk/newstest.php but when you click on more the next page does not load when I try to format the date (in the same way!).


If I don't try and format the date on the detail page it loads fine.

Any ideas what's wrong here?

The code used on the detail page is below as well as a screenshot of the database.
$colname_Recordset1 = "-1";
if (isset($_GET['id'])) {
  $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_GET['id'] : addslashes($_GET['id']);
}
mysql_select_db($database_MJ, $MJ);
$query_Recordset1 = sprintf("SELECT id, copy, date_format(`whatdate`,'%D %M %Y') as `formattedDate`, title, intro FROM news WHERE id = %s", GetSQLValueString($colname_Recordset1, "int"));
$Recordset1 = mysql_query($query_Recordset1, $MJ) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

Open in new window

Screen-shot-2011-12-06-at-14.12..png
Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
is the field whatdate actually date/time data type?

Author

Commented:
Yes it's a date type. It works fine too on that first page. The date manages to format from 2011-10-09 to 9th October 2011 using

SELECT id, copy, date_format(`whatdate`,'%D %M %Y') as `formattedDate`, intro, title FROM news ORDER BY whatdate DESC";

It's just that pretty much the same code on the detail page does not work? very odd...
Most Valuable Expert 2011
Top Expert 2016

Commented:
Please see this article to learn about how to handle DATETIME strings
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Do you suppose sprintf() could be messing with your query?  Those percent signs look interesting!
Most Valuable Expert 2011
Top Expert 2016

Commented:
Here is what I might try just as a proof of concept.

$sql = "SELECT id, whatdate FROM news ORDER BY whatdate DESC LIMIT 1";
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
$dat = $row['whatdate'];
var_dump($dat);
$formattedDate = date('jS F Y', strtotime($dat));
var_dump($formattedDate);
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
>It's just that pretty much the same code on the detail page
so there MUST be a difference in regards to the date value output ...
please re-check the code ...
Most Valuable Expert 2011
Top Expert 2016
Commented:
Here is why your page fails:
http://www.laprbass.com/RAY_temp_brighteye.php
Outputs:
Warning: sprintf() [function.sprintf]: Too few arguments in /home/websitet/public_html/RAY_temp_brighteye.php on line 3
bool(false)

Suggest you raise your error reporting levels so you can see what is going on in the scripts.  These two lines will help.
ini_set('display_errors', TRUE);
error_reporting(E_ALL);

See the type specifiers here: http://php.net/manual/en/function.sprintf.php
<?php // RAY_temp_brighteye.php
error_reporting(E_ALL);
$query_Recordset1 = sprintf("SELECT id, copy, date_format(`whatdate`,'%D %M %Y') as `formattedDate`, title, intro FROM news WHERE id = %s", 3);
var_dump($query_Recordset1);

Open in new window

Author

Commented:
Thanks for that,

I'll look into that when I get a moment.

Author

Commented:
Thanks for your help Ray, I have given up on this though due to time constraints. Cheers though.