Solved

Date formatting not working

Posted on 2012-04-05
1
207 Views
Last Modified: 2012-04-24
I have date formatting for my blog working fine on the initial page using...

mysql_select_db($database_GrandRoofing, $GrandRoofing);
$query_Recordset1 = "SELECT id, intro, title, date_format(`eventdate`,'%D %M %Y') as `formattedDate`, copy, images, category FROM news ORDER BY eventdate DESC";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $GrandRoofing) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

Open in new window


However, when I try to use it on a results page I get a 'Query was empty' error'

This code works fine without formatting..

$colname_Recordset1 = "-1";
if (isset($_GET['id'])) {
  $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_GET['id'] : addslashes($_GET['id']);
}
mysql_select_db($database_GrandRoofing, $GrandRoofing);
$query_Recordset1 = sprintf("SELECT id, intro, title, eventdate, copy, images, category FROM news WHERE id = %s", GetSQLValueString($colname_Recordset1, "int"));
$Recordset1 = mysql_query($query_Recordset1, $GrandRoofing) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

Open in new window


but when I try to format it like the first bit of code the error occurs...

$colname_Recordset1 = "-1";
if (isset($_GET['id'])) {
  $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_GET['id'] : addslashes($_GET['id']);
}
mysql_select_db($database_GrandRoofing, $GrandRoofing);
$query_Recordset1 = sprintf("SELECT id, intro, title, date_format(`eventdate`,'%D %M %Y') as `formattedDate`, copy, images, category FROM news WHERE id = %s", GetSQLValueString($colname_Recordset1, "int"));
$Recordset1 = mysql_query($query_Recordset1, $GrandRoofing) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

Open in new window



Any ideas why the formatting is causing this error?
0
Comment
Question by:BrighteyesDesign
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37812447
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

No really good ideas what might be wrong but I can tell you the easy way to handle this sort of thing.  Store all the dates in ISO-8601 format (MySQL DATE or DATETIME column type).  And use the ISO-8601 representations for all internal purposes.  In PHP, this is date('c').  Then when you want to reformat a date for presentation, use strtotime() and date() to turn the ISO format into a nice, human-readable format.  If you want day, month and year, it might look something like this...
$query_Recordset1 = "SELECT id, intro, title, eventdate, copy, images, category FROM news ORDER BY eventdate DESC";
$Recordset1 = mysql_query($query_Recordset1) or die("FAIL: $query_Recordset1<br/>" . mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$formattedDate = date('j M Y', strtotime($row_Recordset1['eventdate']));

Open in new window

0

Featured Post

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question