0

Usingthe following shortcode function, I have the code building a table by querying the DB. That all works fine. BUT when I add any type of formatting to the TimeStamp column such as DATE_FORMAT or TIME the output for that column only goes blank. No errors are in the console to look at. I am trying to extract only the time and put it in a 12 hour format instead of the typical TIMESTAMP format of date and time in the 24hr set up.

Is there a conflict with some of the PHP that causes it not to display the timestamp?

 function test2() {
        global $wpdb;
    $results = $wpdb->get_results(
    "SELECT `name`, 
        `partysize`, 
        `phonenumber`, 
        `emailaddress`, 
        DATE_FORMAT(`Time_stamp`, '%h %i' ), 
        `Wait`, 
        `currentstatus` 
    FROM mytablename m 
        RIGHT JOIN (
            SELECT wdt_ID, 
            CONCAT(
                ROUND(Time_to_sec(TIMEDIFF (NOW() ,`Time_stamp`))/60,0), 
                ' Min'
                ) AS Wait FROM mytablename
            ) as t on m.wdt_ID = t.wdt_ID 
    WHERE Time_stamp >= date_sub(now(),interval 3 hour)"
    );  

        if(!empty($results))
        {
            echo "<table width='100%' border='0' style='display:inline-table'>
            <th>Name</th>
            <th>Party Size</th>
            <th>Phone Number</th>
            <th>Email Address</th>
            <th>Time Stamp</th>
            <th>Status</th>
            <th>Wait</th>
            <th>Action</th>     ";
            echo "<tbody>";      
            foreach($results as $row){ 
                echo "<tr>";
                //echo "<td>" . $row->wdt_ID . "</td>";
                echo "<td>" . $row->name . "</td>";
                echo "<td>" . $row->partysize . "</td>";
                echo "<td>" . $row->phonenumber . "</td>";
                echo "<td>" . $row->emailaddress . "</td>";
                echo "<td>" . $row->Time_stamp . "</td>";
                echo "<td id='tdid_".$row->wdt_ID."'>" . $row->currentstatus . "</td>";
                echo "<td>" . $row->Wait . "</td>";
                echo '<td> <a href="javascript:void(0)" class="button seated-btn" onclick="seatclick('.$row->wdt_ID.')" data-id="'.$row->wdt_ID.'"></a></td>';
                echo "</tr>";
            }
            echo "</tbody>";
            echo "</table>"; 
        }
    }
    add_shortcode('test2', 'test2');
  • 1
    Would it not make more sense to format the date in PHP instead of making the database do it? Keep in mind your shortcode is broken, shortcodes return their output as a string variable, they're not supposed to echo it out. Your shortcode won't work inside nested shortcodes and blocks and will break, it'll also break XMLRPC/RSS/REST/etc for the same reason – Tom J Nowell Sep 24 '20 at 22:52
  • @TomJNowell interesting. The whole thing works and builds the table ok if I dont have the TIME or DATE_FORAMT function on. Should I expect it not to? Or is it prone to future breaking? As for the PHP how would I do that? – michael Sep 24 '20 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.