PHP/WordPress: include hours & minutes in ascending list

I’m modifying a WordPress plugin that another dev wrote a couple years back. In the PHP code, he’s created a $now variable to get the current time. Then further down, we’re displaying a list of content filtered in ascending order. The problem I’m running into is that if there are multiple posts on the same day, it’s not filtering them in ascending order according to the hours and minutes; only the day.

You can see what I’m talking about here: http://alhughesauction.com/upcoming-auctions
The first two auctions are on the same day, but the second one should actually display first since it’s at 11am and the other is at 3pm.

Read More

This is how the $now is defined:

$now = "'" . date("Y-m-d", strtotime("now-5hours")) . "'";

As per the PHP manual, I tried adding -g to "Y-m-d", but that didn’t work.

Here is where we’re grabbing the data from the database:

$sql = "SELECT * FROM " . AH_DATA . " WHERE date >= {$now} ORDER BY date ASC" . $limit;

I would venture to guess that I simply need to get the $now variable to add hours and minutes, but I’m still learning the ropes of PHP 🙂

Related posts

Leave a Reply

1 comment

  1. As Bhumi said use date("Y-m-d H:i:s"); for $now

    $now = "'" . date("Y-m-d H:i:s", strtotime("now-5hours")) . "'";
    

    Also the datatype of date column should be timestamp, if it is not use this query for ordering in ascending order

    $sql = "SELECT timestamp( `date` ) as 'date', * FROM " . AH_DATA . " 
    WHERE `date` >= {$now} ORDER BY 1 ASC" . $limit;
    

    date is reserved word in mysql use ` around column name to use it like column name

    Edit

    Try this query

    $sql = "SELECT timestamp(CONCAT(`date`," ",`time` ) as 'date', * FROM " . AH_DATA . " 
    WHERE `date` >= {$now} ORDER BY 1 ASC" . $limit;