Help with date format

If anyone could spot where I’m going wrong here, that would be much appreciated.

I have a field, dateadded, in a table, that just time stamps when a new record is added.

I’m just trying to display that date on pages in the format 27th August 2010.

I have a query that looks like this :

$currentPage = $_SERVER["PHP_SELF"];

$colname_DateFormat = "1";
if (isset($_Get['packageID'])) {
  $colname_DateFormat = (get_magic_quotes_gpc()) ? $_Get['packageID'] : addslashes($_Get['packageID']);
mysql_select_db($database_connPackages, $connPackages);
$query_DateFormat = sprintf("SELECT date_format(dateadded, '%%D %%b %%Y') as formatted_date FROM packages WHERE packageID = %s", $colname_DateFormat);
$DateFormat = mysql_query($query_DateFormat, $connPackages) or die(mysql_error());
$row_DateFormat = mysql_fetch_assoc($DateFormat);
$totalRows_DateFormat = mysql_num_rows($DateFormat);

And am using this to echo the date :

<?php echo $row_DateFormat['formatted_date']; ?>

But its just drawing a blank…

Any ideas?

okay, so, try it without doubling up the percent signs

If I echo the query using :

<?php echo "Here is my query: ".$query_DateFormat; ?>

It returns :

SELECT date_format(dateadded, ‘%D %b %Y’) as formatted_date FROM packages WHERE packageID = 1

So presumably that 1 at the end just needs to be the packageID for each record. (If I change the 1 to an actual packageID it works).

The way I understood it was that this part :

$colname_DateFormat = "1"; 
if (isset($_Get['packageID'])) { 
  $colname_DateFormat = (get_magic_quotes_gpc()) ? $_Get['packageID'] : addslashes($_Get['packageID']); 

is giving it a default value of 1, unless it can get a packageID, and use that instead?

So not sure why that isn’t doing that.

If I put

SELECT date_format(dateadded, ‘%%D %%b %%Y’) as formatted_date FROM packages

into the SQL part of phpMyAdmin, it returns

%D %b %Y

for each record.

Agreed, much better in php that buried deep inside MySQL.

Thanks Denny - that was it.

So I’ve got it working for the details page :

But not sure how I’d go about it on the results page :

the double %'s are to fool sprintf into ignoring the % (which is a control character). Leave the doubles in.

Your script is expecting a $_GET variable to be passed on the URL.
I see that it’s called educational-tours4.php
Try putting on the end of the url: educational-tours4.php?packageID=2
(or whatever number you want to see)

Not sure why it didn’t work on the results page.

But after all that, I’ve found a much simpler solution…

Turns out I just needed to change :

<?php echo($row_WADApackages['dateadded']); ?>


<?php echo date('j F Y',strtotime($row_WADApackages['dateadded'])); ?>

And not bother with all that stuff creating the formatted_date field.

Glad to hear it worked! Use the very same method on the results page code. Add the “formatted_date” column to the mysql query for the results page.

if (isset($_Get['packageID'])) { 
  $colname_DateFormat = (get_magic_quotes_gpc()) ? $_Get['packageID'] : addslashes($_Get['packageID']); 

I think the get variable has to be all caps as in:


Oh, hold on. That page is a results page :

So that’s going to need to be different to a details page…

So if I have a page like this :

How would I change those long winded date formats to, for example, 27th August 2010?

Although, having said that, here’s the details page still drawing a blank :

what happens when you run the SELECT statement outside of php?

OK - thanks for looking.

By the way - when I changed it to single % signs in phpMyAdmin, it returned the correct dates.

But if I change that in the PHP on the web page, the page just errors out with :

Warning: sprintf() [function.sprintf]: Too few arguments in /home/travelco/public_html/educational-tours4.php on line 101
Query was empty

sorry, i can’t help you, i don’t do php and this isn’t the php forum

perhaps you could flag this thread and ask a moderator to move it to the php forum