We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 52464
    • 14 Posts
    I'm building an e-commerce site with custom code and custom tables. When an user purchases an item, I need the database to insert the current date in the order table using the MySql function 'CURDATE()' as the value for the date field. Instead of inserting the current date, it only inserts 0000-00-00.

    My code looks like this (excerpt):
        $orderFields = array('idClient' => $_SESSION['loggedIn']['id'], 'totalAmount' => $totalAmount, 'paymentType' => 'debit card', 'orderDate' => 'CURDATE()');
        $order = $modx->newObject('Order', $orderFields);
        $oSave = $order->save();
    


    Momentarily, I'm obtaining the current date with PHP and inserting it directly:

        $orderDate = date('Y-n-j');
        $orderFields = array('idClient' => $_SESSION['loggedIn']['id'], 'totalAmount' => $totalAmount, 'paymentType' => 'debit card', 'orderDate' => $orderDate);
    


    But, how can I access the MySql 'CURDATE()' function from a snippet inside Modx?
    For this case, it doesn't produce any difference in the result; but I guess that there are other cases in which you definitely need to use a particular MySql function.
    How can I access them?

    Thank you!!
      • 3749
      • 24,544 Posts
      TBH, I don't see anything wrong with what you're doing now, but this might help for cases where you want to bypass MODX by using PDO:

      https://bobsguides.com/blog.html/2014/12/01/bypassing-modx-to-write-directly-to-the-database/

      BTW, in a custom table, I'd be tempted to store a current timestamp with PHP's time() function rather than a human-readable date, unless you never need to do date arithmetic on it.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting