Skip to content
Advertisement

How to limit query by date

I try to limit the query to show only the results from the last 7 days (always from Friday), i.e. from the previous Friday from (12:01) to the current Friday (11:59). I’m using woocommerce wp_wc_order_product_lookup and want to show exactly that orders from this range

Advertisement

Answer

This is how you can get SQL Select.

<?php    
$datetime1 = new DateTimeImmutable('last friday 12:00:00');
$datetime2 = $datetime1->modify('+ 1 week');
$sql = 'SELECT * wp_wc_order_product_lookup
        WHERE date_created BETWEEN 
        ' . $datetime1->format("Y-m-d H:i:s") 
        . ' AND 
        ' . $datetime2->format("Y-m-d H:i:s")
    ;

echo $sql; // result: SELECT * table_name WHERE datetime_column_name BETWEEN 2020-04-10 12:00:00 AND 2020-04-17 12:00:00
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement