?
Solved

Date formatting not working

Posted on 2012-04-05
1
Medium Priority
?
211 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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