Solved

Date formatting not working

Posted on 2012-04-05
1
191 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
1 Comment
 
LVL 108

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Creating and Managing Databases with phpMyAdmin in cPanel.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now