Solved

Sorting by date with php/mysql broke?

Posted on 2004-04-02
1
541 Views
Last Modified: 2012-08-14
I'm currently trying to display a mysql db and order the results by date...  With my current code, the output is put in date order to about 90%, but some of the dates are out of order for some reason.  I've been trying to figure this out for a few days now and I need help! =)  Below is my php source and a link to my php page...

<?php
$db = mysql_connect("localhost","xxxx","xxxxxxxxxxxx") or die("Problem connecting");
mysql_select_db("mikegood_test") or die("Problem selecting database");

// query one
$query = "SELECT date_format(date,'%M %D, %Y') as date, col2, col3 FROM test ORDER BY date ASC";

// query the database for lans in the next two weeks - broke
$query2 = "SELECT date_add('2004-04-01', INTERVAL 31 DAY) as date, col2, col3 FROM test ORDER BY date ASC";

// datesub()
$query3 = "SELECT DATE_SUB('2004-04-15', INTERVAL 31 DAY);
        -> '2004-03-30'";

$result = mysql_query($query) or die ("Query failed");
//get the number of rows in our result to use in the for loop
$numofrows = mysql_num_rows($result);
?>

<?php
echo "<table border=\"1\">\n";
echo "<tr bgcolor=\"lightblue\"><td>date</td><td>col2</td><td>col3</td></tr>\n";
for($i = 0; $i < $numofrows; $i++) {
    $row = mysql_fetch_array($result); //get a row from the result set
    if($i % 2) { // if there is a remainder
        echo "<tr bgcolor=\"yellow\">\n";
    } else { //if there isn't a remainder
        echo "<tr bgcolor=\"white\">\n";
    }
    echo "<td>".$row['date']."</td><td>".$row['col2']."</td><td>".$row['col3']."</td>\n";
    echo "</tr>\n";
}
//close the table
echo "</table>\n";
?>

<br><br>

<?
$date = date("D j M Y, G:ia");
//$date = date("Y
echo $date;
?>

this page can be viewed live from: http://mikegood.net/displaytest.php
0
Comment
Question by:mgood16
1 Comment
 
LVL 17

Accepted Solution

by:
leannonn earned 125 total points
ID: 10747197
> SELECT date_format(date,'%M %D, %Y') as date, col2, col3 FROM test ORDER BY date ASC

The sorting does not work as expected because you sort the results on the modified/formated field `date`. You might want to try this one:
---
SELECT date_format(date,'%M %D, %Y') as my_date, col2, col3 FROM test ORDER BY date ASC
---

This way you sort it by the original `date` field, not the formatted one.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Importing and exporting data Magento 1.x ? 4 83
PHP strip quotes and line feeds out of array 13 57
MySQL  on Tomcat 8 43
How can i make performance tuning to my sql query? 6 46
This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

17 Experts available now in Live!

Get 1:1 Help Now